Home  >  Article  >  Backend Development  >  Detailed explanation of three methods of displaying MySQL data in PHP_PHP Tutorial

Detailed explanation of three methods of displaying MySQL data in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:52:18692browse

Yesterday's program was like this:
$link=mysql_connect("localhost","root","previous administrator password");
if(!$link) echo " No connection successful!";
else echo "Connection successful!";
mysql_select_db("infosystem", $link);
$q = "SELECT * FROM info"; NAMES GB2312");
$rs = mysql_query($q, $link);
if(!$rs){die("Valid result!");}
echo "

" ;
echo "";

while($row = mysql_fetch_row($rs)) echo "";
echo "
Department nameEmployee namePC name
$row[1]$row[2]$row[3]
"; mysql_close($link);
? >
The red part in the program is the key to displaying the data in MySQL. Here, you can replace the red part with the other two methods and achieve the same effect.

while($row = mysql_fetch_object($rs)) echo "$row->depart$row->ename$row-pcname";
while($row = mysql_fetch_array($rs)) echo "$row[depart]< ;/td>$row[ename]$row[pcname]";
Briefly explain:

A row of data columns returned by the mysql_fetch_row() method is stored in an array unit, and the offset starts from 0. For example, if you want to return the data of the second field, you should write it as $row[1] instead of $row[2].

Mysql_fetch_array() is similar to mysql_fetch_row(). The returned data is also stored in an array. The difference is that we should use field names to access the array instead of offsets, such as $row[ depart].

Mysql_fetch_object() returns not an array but an object. We should use the operation method on the object to read the data, for example: $row->depart.

You can choose a method according to your own needs to replace the red part in the program to read MySQL data.

http://www.bkjia.com/PHPjc/318949.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/318949.htmlTechArticleYesterday’s program was like this: ?php $link=mysql_connect("localhost","root","before Administrator password"); if(!$link)echo"No connection successful!"; elseecho"Connection successful!"; mysql_select_db...
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