Home  >  Article  >  Backend Development  >  Three ways to directly read database information_PHP tutorial

Three ways to directly read database information_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:36:561127browse

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.

The following is the quoted content:

$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 error reports */
if(!$dbh){die("error");}
/* The die() function means to send the string in 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" ;Webpage Teaching Network http://www.webjx.com
/* Define variable q, "SELECT * FROM abc" is a SQL statement, which means to read the data in table abc*/
?>





$rs = mysql_query($q, $dbh);
/* Define the variable rs, the function mysql_query() means: send the query string for MySQL to do related processing or execution. Since PHP is executed from right to left, the value of rs is the value returned by the server after running the mysql_query() function*/
if(!$rs){die("Valid result!");}
echo "

";
echo "";
while($row = mysql_fetch_row($rs)) echo "";
/* Define the quantitative variable (array) row, and use a while loop to write out the data one by one.
The function mysql_fetch_row() means: split the query result $rs into an array variable in a single column.
$row The positions of [0] and $row[1] can be changed*/
echo "
IDName
$row[0]$row[1]
";
?>





$rs = mysql_query($q, $dbh);
while($row = mysql_fetch_object($rs)) echo "$row->id $row->name
";
/* id and name can be replaced Location*/
?>





$rs = mysql_query($q, $dbh);
while($row = mysql_fetch_array($rs)) echo "$row[id ] $row[name]
";
/* id and name can be changed */
?>

@mysql_close($dbh);
/* Close the connection to the mysql database*/
? >

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486621.htmlTechArticleThe function of this code is: Connect to a mysql server with the url address localhost and port 3306. The account number of the mysql server is "root" and the password is "9999". There is...
on the mysql server
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