Slide môn học PHP session 6 handling databases with PHP

24 4 0
  • Loading ...
1/24 trang

Thông tin tài liệu

Ngày đăng: 30/11/2016, 22:11

Handling Databases with PHP Session 15 Review       A variable can store only one value at a time An array is a variable that can store a set of values of the same data type We can combine the element values of two or more arrays This process is called as merging arrays In a single-dimensional array, the element includes only one level of key value pairs In the multidimensional array, each element is an array Each element requires an array name and multiple set of indices Multidimensional arrays are arrays that store another array within it PHP / Session 15 / Slide of 24 Objectives      Database APIs Connecting to a database Data access functions Performing SQL queries using PHP Building HTML tables using SQL queries PHP / Session 15 / Slide of 24 Database APIs      Allows developers to write applications those are movable or easily accessible between the database products Creates a common set of libraries for the program when used with programs Native-Interface, ODBC, JDBC, and CORBA are the common database APIs PHP supports MySQL database for accessing data from the database server MySQL does not have its own APIs PHP / Session 15 / Slide of 24 Connecting to a Database   Uses three arguments to connect PHP to MySQL such as server host name, user name, user password Connectivity of PHP to MySQL are divided into three steps:    Connection with the MySQL server Working with the databases Closing the database connection PHP / Session 15 / Slide of 24 Connection with the MySQL server - I   Connects with the MySQL server using mysql_connect() function This function takes three arguments: Hostname  Database username  Database user password  PHP / Session 15 / Slide of 24 Connection with the MySQL server - II  Syntax for connecting with the MySQL server is: $link_id = mysql_connect(“host_name”, “user_name”,“password”); Where,     host_name – Specifies the server or the host name user_name – Specifies the user name of MySQL password – Specifies the password for the MySQL user link_id - Specifies the return value of the server connection PHP / Session 15 / Slide of 24 Example for connection with the MySQL server  For example, to connect PHP to MySQL server PHP / Session 15 / Slide of 24 Working with the Databases   Establish a connection with the MySQL server Needs to perform basic PHP functions while working with the database such as     mysql_list_dbs() mysql_select_db() mysql_list_tables() mysql_num_rows() PHP / Session 15 / Slide of 24 mysql_list_dbs() function   Displays all the databases present in the server Syntax for this function is: $result = mysql_list_dbs($link_id); Where, link_id - Specifies the return value of the server connection  result – Assigns the value of the function  PHP / Session 15 / Slide 10 of 24 mysql_select_db() function   Selects a database from the server Syntax for this function is: $result = mysql_select_db(“database_name”, $link_id); Where,    database_name – Specifies the name of the database link_id – Specifies the return value of the server connection result – Assigns the value of the function PHP / Session 15 / Slide 11 of 24 mysql_list_tables() function   Displays the list of tables present in the selected database Syntax for this function is: $result = mysql_list_tables(“database_name”, $link_id); Where,  database_name – Specifies the database name  link_id – Specifies the return value of the database connection  result – Stores the result of the function PHP / Session 15 / Slide 12 of 24 mysql_num_rows() function   Shows the number of rows present in the table Syntax for this function is: $num_rows = mysql_num_rows($result); Where, result – Specifies the argument taken for displaying the number of rows  num_rows – Stores the result of the function  PHP / Session 15 / Slide 13 of 24 Closing the connection   Closes the connection with the MySQL server by using the mysql_close() function Syntax for this function is : mysql_close($link_id); Where,  link_id - Specifies the return value of the server connection PHP / Session 15 / Slide 14 of 24 Data Access Functions   Uses MySQL functions in PHP for accessing data from the tables of the database Data are accessed from the tables using the following mysql functions Those are:       mysql_query() mysql_fetch_array() mysql_fetch_row() mysql_fetch_field() mysql_field_len() mysql_num_fields() PHP / Session 15 / Slide 15 of 24 Example for data access functions -I  For example, to display the records of the table from the USER database using the data access functions The above codes will display the records of the USER_DETAILS table from the USER database PHP / Session 15 / Slide 18 of 24 Performing SQL queries using PHP - I  For example, create a table using SQL commands in PHP scripts PHP / Session 15 / Slide 20 of 24 Building HTML tables using SQL queries   HTML supports the database application components for accessing the database For example, to display all the records of the user_contact table from the user database by using HTML table structure PHP / Session 15 / Slide 21 of 24 Example for HTML tables using SQL queries - I PHP / Session 15 / Slide 23 of 24 Summary     Database APIs allows the developers to write applications that are movable or easily accessible between the database products Connection with the server is done by using mysql_connect() function Functions used with the database are mysql_list_dbs(), mysql_select_db(), mysql_list_tables(), mysql_num_rows() Connection with the MySQL server is done with mysql_close() function PHP / Session 15 / Slide 24 of 24 [...]... “Address:”.$row[USER_ADDRESS]; } PHP / Session 15 / Slide 17 of 24 Example for data access functions - III if(mysql_num_rows($sqlquery) The above codes will display the records of the USER_DETAILS table from the USER database PHP / Session 15 / Slide 18 of 24 Performing SQL queries using PHP - I  For example, create a table using SQL commands in PHP scripts < ?php $server = “”; $username... the result of the function  PHP / Session 15 / Slide 13 of 24 Closing the connection   Closes the connection with the MySQL server by using the mysql_close() function Syntax for this function is : mysql_close($link_id); Where,  link_id - Specifies the return value of the server connection PHP / Session 15 / Slide 14 of 24 Data Access Functions   Uses MySQL functions in PHP for accessing data from... mysql_num_fields() PHP / Session 15 / Slide 15 of 24 Example for data access functions -I  For example, to display the records of the table from the USER database using the data access functions < ?php $server = “”; $username = “root”; $password = “”; $connect_mysql = mysql_connect($server, $username, $password); if($connect_mysql) echo “Connection established”; else die(“Unable to connect”); PHP / Session 15 / Slide. .. create a table”); ?> PHP / Session 15 / Slide 20 of 24 Building HTML tables using SQL queries   HTML supports the database application components for accessing the database For example, to display all the records of the user_contact table from the user database by using HTML table structure PHP / Session 15 / Slide 21 of 24 Example for HTML tables using SQL queries - I < ?php $server = “”;... PHP / Session 15 / Slide 23 of 24 Summary     Database APIs allows the developers to write applications that are movable or easily accessible between the database products Connection with the server is done by using mysql_connect() function Functions used with the database are mysql_list_dbs(), mysql_select_db(), mysql_list_tables(), mysql_num_rows() Connection with the MySQL server is done with. .. function PHP / Session 15 / Slide 11 of 24 mysql_list_tables() function   Displays the list of tables present in the selected database Syntax for this function is: $result = mysql_list_tables(“database_name”, $link_id); Where,  database_name – Specifies the database name  link_id – Specifies the return value of the database connection  result – Stores the result of the function PHP / Session 15 / Slide. .. = “root”; $password = “”; $connect_mysql = mysql_connect($server, $username, $password); if($connect_mysql) echo “Connection established”; else die(“Unable to connect”); PHP / Session 15 / Slide 19 of 24 Performing SQL queries using PHP - II $mysql_db = mysql_select_db(“USER”); if($mysql_db) echo “Connected to the database”; else die(“Unable to connect to database”); $sql_table=”CREATE TABLE USER_CONTACT(“.“USER_ID... = “”; $username = “root”; $password = “”; $connect_mysql = mysql_connect($server, $username, $password); if($connect_mysql) echo “Connection established”; $mysql_db = mysql_select_db(“USER”); PHP / Session 15 / Slide 22 of 24 Example for HTML tables using SQL queries - II if($mysql_db) echo “Connected to the database”; echo “”; echo “ USER_ID USER_NAME... mysql_connect() function Functions used with the database are mysql_list_dbs(), mysql_select_db(), mysql_list_tables(), mysql_num_rows() Connection with the MySQL server is done with mysql_close() function PHP / Session 15 / Slide 24 of 24 ... $username = “root”; $password = “”; $connect_mysql = mysql_connect($server, $username, $password); if($connect_mysql) echo “Connection established”; else die(“Unable to connect”); PHP / Session 15 / Slide 16 of 24 Example for data access functions - II $mysql_db = mysql_select_db(“USER”); if($mysql_db) echo “Connected to the database”; else die(“Unable to connect to database”); $sqlquery = mysql_query(“SELECT
- Xem thêm -

Xem thêm: Slide môn học PHP session 6 handling databases with PHP , Slide môn học PHP session 6 handling databases with PHP , Slide môn học PHP session 6 handling databases with PHP

Gợi ý tài liệu liên quan cho bạn

Nạp tiền Tải lên
Đăng ký
Đăng nhập