Home  >  Article  >  Backend Development  >  PHP example program: three methods to directly read database information_PHP tutorial

PHP example program: three methods to directly read database information_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:29:31700browse

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");
/* 定义变量dbh , mysql_connect()函数的意思是连接mysql数据库, "@"的意思是屏蔽报错 */
if(!$dbh){die("error");}
/* die()函数的意思是将括号里的字串送到浏览器并中断PHP程式 (Script)。括号里的参数为欲送出的字串。 */
@mysql_select_db("ok", $dbh);
/* 选择mysql服务器里的一个数据库,这里选的数据库名为 ok */
$q = "SELECT * FROM abc";
/* 定义变量q, "SELECT * FROM abc"是一个SQL语句,意思是读取表abc中的数据 */
?>





$rs = mysql_query($q, $dbh);
/* 定义变量 rs ,函数mysql_query()的意思是:送出 query 字串供 MySQL 做相关的处理或者执行.由于php是从右往左执行的,所以,rs的值是服务器运行mysql_query()函数后返回的值 */
if(!$rs){die("Valid result!");}
echo "

";
echo "";
while($row = mysql_fetch_row($rs)) echo "";
/* 定义量变(数组)row,并利用while循环,把数据一一写出来.
函数mysql_fetch_row()的意思是:将查询结果$rs单列拆到阵列变数中.
$row[0] 和 $row[1] 的位置可以换*/
echo "
IDName
$row[0]$row[1]
";
?>





$rs = mysql_query($q, $dbh);
while($row = mysql_fetch_object($rs)) echo "$row->id $row->name
";
/* id和name可以换位置 */
?>





$rs = mysql_query($q, $dbh);
while($row = mysql_fetch_array($rs)) echo "$row[id]  $row[name]
";
/* id和name可以换位置 */
?>

@mysql_close($dbh);
/* 关闭到mysql数据库的连接 */
?>

$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 reporting*/
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" ;
/* 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 function mysql_query() means: send the query string for MySQL to do related processing or execution. Since php is executed from right to left, so, 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 "$row[0]";
/* Define the quantitative variable (array) row and use while loop, write out the data one by one.
The function mysql_fetch_row() means: split the query result $rs single column into array variables.
The positions of $row[0] and $row[1] can be Replace */
echo "
IDName
$row[1]
";
?>





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





$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*/
?>

http://www.bkjia.com/PHPjc/531675.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/531675.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