An example of using PHP to read database data. When I learned "ten days to learn PHP", I encountered a problem about database connection due to lack of knowledge. So I asked a friend to help me write a small PHP program. when I learned "ten days to learn PHP", when I saw a problem about database connection, due to lack of knowledge, encountered difficulties. So I asked a friend to help write a PHP applet and posted it here, hoping to help you. Note is written by myself.
The functions of this code are as follows:
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.
$ 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"
Bytes. So I asked a friend to help write a PHP applet...
";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.