Steps for accessing a database interaction with PHP

Source: Internet
Author: User

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"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.