PHP instance program: three methods for directly reading database information _ PHP Tutorial

Source: Internet
Author: User
PHP instance program: three methods for directly reading database information. The function of this code is to connect to a mysql server whose url address is localhost and Port is 3306. The account of the mysql server is root and the password is 9999. The following code is available on the mysql server:

Connect to a mysql server whose url is localhost and Port is 3306. The account and password of the mysql server are "root" and "9999 ". The mysql server has a database "OK" and a table "abc" in the database. The abc table has two columns: "id" and "name". The column names read all the data in abc.

Reference content is as follows:

$ Dbh = @ mysql_connect ("localhost: 3306", "root", "9999 ");
/* Define the variable dbh. the mysql_connect () function means to connect to the mysql database, and "@" means to block the error */
If (! $ Dbh) {die ("error ");}
/* The die () function means to send the strings in brackets to the browser and interrupt the PHP program (Script ). The parameters in the brackets are the strings to be sent. */
@ Mysql_select_db ("OK", $ dbh );
/* Select a database on the mysql server. the database name is OK */
$ Q = "SELECT * FROM abc ";
/* Define the variable q. "SELECT * FROM abc" is an SQL statement that reads data in table abc */
?>





$ Rs = mysql_query ($ q, $ dbh );
/* Define the rs variable. the Function mysql_query () indicates that the query string is sent for MySQL to perform related processing or execution. since php is executed from right to left, the rs value is the value returned after the server runs the mysql_query () function */
If (! $ Rs) {die ("Valid result! ");}
Echo"







";Echo" ";While ($ row = mysql_fetch_row ($ rs) echo" ";/* Define the volume change (array) row and write the data one by one using the while loop.The mysql_fetch_row () function means to split the query result $ rs into an array variable.$ Row [0] and $ row [1] can be changed */Echo"
ID Name
$ Row [0] $ Row [1]
";
?>





$ Rs = mysql_query ($ q, $ dbh );
While ($ row = mysql_fetch_object ($ rs) echo "$ row-> id $ row-> name
";
/* The id and name can be changed */
?>





$ Rs = mysql_query ($ q, $ dbh );
While ($ row = mysql_fetch_array ($ rs) echo "$ row [id] $ row [name]
";
/* The id and name can be changed */
?>

@ Mysql_close ($ dbh );
/* Close the connection to the mysql database */
?>

Connects to a mysql server whose url is localhost and Port is 3306. The account and password of the mysql server are "root" and "9999 ". Mysql server has...

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.