Home  >  Article  >  Backend Development  >  An example of using PHP language to read database data_PHP tutorial

An example of using PHP language to read database data_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:58:48907browse

In the process of learning "Learn PHP in Ten Days", when I saw the problem about database connection, I encountered difficulties due to lack of knowledge. So I asked a friend to help me write a small PHP program and posted it here. I hope it will be helpful to everyone. The annotations are written by myself.

The function of this code is:

Connect to a mysql server with url address localhost and port 3306. The account number of the mysql server is "root" and the password is "9999". There is a database ok on the mysql server, and there is a table abc in the database. Table abc has two columns in total. The column names are "id" and "name". Read out 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, "@" means to block errors */

if(!$dbh){die("error");}

/* The die() function means to send the string in the brackets to the browser and interrupt the PHP program (Script). The parameters in brackets are the string to be sent. */

@mysql_select_db("ok", $dbh);

/* Select a database in the mysql server. The database name selected here is ok */

$q = "SELECT * FROM abc";

/* Define variable q, "SELECT * FROM abc" is a SQL statement, which means reading data in table abc */

?>








$rs = mysql_query($q, $dbh);

/* Define the variable rs. The meaning of the function mysql_query() is to send the query string for MySQL to perform related processing or execution. Since PHP is executed from right to left, the value of rs is returned after the server runs the mysql_query() function. value*/

if(!$rs){die("Valid result!");}

echo "

";

echo "";

while($row = mysql_fetch_row($rs)) echo " ";

/* Define the quantitative variable (array) row, and use the while loop to write out the data one by one.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631979.htmlTechArticleIn the process of learning "Learn PHP in Ten Days", when I saw the question about database connection, due to my knowledge We encountered difficulties due to shortcomings. So I asked a friend to help me write a small PHP program, here...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
IDName
$row[0]$row[1]