Three methods for php to read mysql Databases

Source: Internet
Author: User
Mysql can read databases in three ways. For more information, see.

Mysql can read databases in three ways. For more information, see.

The Code is as follows:
$ Dbh = @ mysql_connect ("localhost: 3306", "root", "000000 ");
/* 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 */
?>


The Code is as follows:
$ Rs = mysql_query ($ q, $ dbh );
/* Define the variable rs. The function mysql_query () indicates that the query string is sent for MySQL to perform related processing or execution. Because php is executed from right to left
Therefore, 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]
";
?>


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


The Code is as follows:
$ 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 */
?>

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.