Three methods for displaying Mysql Data in PHP

Source: Internet
Author: User

Yesterday's Program Yes:
<? PHP
$ 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 ";
Mysql_query ("set names gb2312 ");
$ Rs = mysql_query ($ Q, $ link );
If (! $ RS) {die ("valid result! ");}
Echo "<Table> ";
Echo "<tr> <TD> Department name </TD> <TD> employee name </TD> <TD> PC name </TD> </tr> ";
While ($ ROW = mysql_fetch_row ($ RS )) echo "<tr> <TD> $ row [1] </TD> <TD> $ row [2] </TD> <TD> $ row [3] </TD> </tr> ";
Echo "</table> ";
Mysql_close ($ link );
?>
The red part in the program is the key to displaying Mysql Data. Here, you can use the other two methods to replace the red part and achieve the same effect.

While ($ ROW = mysql_fetch_object ($ RS )) echo "<tr> <TD> $ row-> depart </TD> <TD> $ row-> ename </TD> <TD> $ row-pcname </TD> </tr> ";
While ($ ROW = mysql_fetch_array ($ RS )) echo "<tr> <TD> $ row [depart] </TD> <TD> $ row [ename] </TD> <TD> $ row [pcname] </TD> </tr> ";
Briefly describe:

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

Similar to mysql_fetch_array () and mysql_fetch_row (), the returned data is stored in an array. The difference is that we should use the field to access the array instead of using the offset, for example, $ row [depart].

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

You can select a method to read Mysql Data instead of the red part in the program as needed.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.