Three methods for php to read mysql database: bitsCN.com
$ 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 */
?>
$ 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"
ID |
Name |
";While ($ row = mysql_fetch_row ($ rs) echo"
$ Row [0] |
$ Row [1] |
";/* 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"
";
?>
$ 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 */
?>
BitsCN.com