Several Methods for PHP to read database information
Source: Internet
Author: User
<?
$ 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 */
?>
<Br/>
<! -- =========== Method 1 =================-->
<Br/>
<?
$ 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 "<Table> ";
Echo "<tr> <TD> id </TD> <TD> name </TD> </tr> ";
While ($ ROW = mysql_fetch_row ($ RS )) echo "<tr> <TD> $ row [0] </TD> <TD> $ row [1] </TD> </tr> ";
/* 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 "</table> ";
?>
<Br/>
<! -- ========= Method 2 =================-->
<Br/>
<?
$ Rs = mysql_query ($ Q, $ DBH );
While ($ ROW = mysql_fetch_object ($ RS) echo "$ row-> ID $ row-> name <br/> ";
/* The ID and name can be changed */
?>
<Br/>
<! -- ========= Method 3 =================-->
<Br/>
<?
$ Rs = mysql_query ($ Q, $ DBH );
While ($ ROW = mysql_fetch_array ($ RS) echo "$ row [ID] $ row [name] <br/> ";
/* The ID and name can be changed */
?>
<! -- =========== Method 3 fastest ===============-->
<?
@ Mysql_close ($ DBH );
/* Close the connection to the MySQL database */
?>
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