Three methods for displaying MySQL data in PHP _ PHP Tutorial

Source: Internet
Author: User
Three methods for displaying MySQL data in PHP are described in detail. Yesterday's program was like this :? Php $ linkmysql_connect (localhost, root, previous administrator password); if (! $ Link) echo no connection successful !; Elseecho connection successful !; The program of mysql_select_db yesterday is as follows:
$ 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"


";Echo" ";While ($ row = mysql_fetch_row ($ rs) echo" ";Echo"
Department name Employee Name PC name
$ Row [1] $ Row [2] $ Row [3]
";
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"$ Row-> depart$ Row-> ename$ Row-pcname";
While ($ row = mysql_fetch_array ($ rs) echo"$ Row [depart]$ Row [ename]$ Row [pcname]";
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.

Why :? Php $ link = mysql_connect ("localhost", "root", "previous administrator password"); if (! $ Link) echo "no connection successful! "; Elseecho" connection successful! "; Mysql_select_db...

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.