The procedure for accessing a database interaction with PHP is as follows:
1. Open the database connection
Mysql_pconnect (host, user name, password );
For example:
$ UseName = "root ";
$ Passwd = "yb ";
$ Sserver = "127.0.0.1: 3306 ";
$ Db_link = @ mysql_connect ($ Sserver, $ useName, $ passwd );
2. Select an Active Database
$ Db = mysql_select_db ("$ dbname ")
Example: $ bresult = mysql_select_db (myphp, $ db_link );
3. Perform the query operation:
Mysql_query ($ SQL)
Example: mysql_query ("select * from student", $ db_link );
4. Get the number drama from the result:
$ Row = mysql_fetch_array ($ rst) or die ("no more records! "); // Retrieve a record
Example: $ row = mysql_fetch_row ($ result)
5. release memory resources
Mysql_free_result ($ rst) or die ("the result resource cannot be released! ");
Example: mysql_free_result ($ result)
6. Shut down the database
@ Mysql_close ($ db_link );
Example of the entire process:
<? Php
$ UseName = "root ";
$ Passwd = "yb ";
$ Sserver = "127.0.0.1: 3306 ";
$ Db_link = @ mysql_connect ($ Sserver, $ useName, $ passwd );
$ Bresult = mysql_select_db (myphp, $ db_link );
$ Result = mysql_query ("select * from student", $ db_link );
$ I = 0;
While ($ row = mysql_fetch_row ($ result ))
{
Print_r ("id =". $ row [0]. "$ nbsp; name =". $ row [1]. "<br/> ");
$ I ++;
}
Mysql_data_seek ($ result, 0 );
Mysql_free_result ($ result );
Print ("$ I ");
@ Mysql_close ($ db_link );
?>
Author: "ITeamsky-Yang Bo's technical space"