Home >Backend Development >PHP Tutorial >In-depth analysis of using mysql_fetch_object() to return query results in the form of objects_PHP tutorial

In-depth analysis of using mysql_fetch_object() to return query results in the form of objects_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:09:001098browse

mysql_fetch_object() is also used to obtain the query data result set, return the current row of data, and automatically slide to the next row. But unlike mysql_fetch_row() and mysql_fetch_array(), it returns an object. The attribute set of this object is the attribute set of the data, and the value of the attribute is The value of this attribute for the current row in the database. The function is defined as follows.

Copy code The code is as follows:

object mysql_fetch_object( int result, int [result_type])

The parameter description is the same as mysql_fetch_array().
The return value is as follows.
Success: An object whose attribute name corresponds to the attribute name in the result set, and the value of the attribute is the corresponding attribute value in the result set.
Failure: false.
The following is an example of using mysql_fetch_object(): querying book information in the Computers data table.
Copy code The code is as follows:


2 3 //Connect and select the database server
4 $connection = mysql_connect ("localhost", "root", "password");
5 mysql_select_db("Books", $connection);
6 //Query data
7 $query="SELECT * FROM Computers ";
8 $query.="WHERE price >= 20";
9                                                                                                                          $query. 12 While ($ Row = MySQL_FETCH_OBJECT ($ Result))
13 {
14 echo "Book Title:" $ row-& gt; "& lt;"
15 echo "Price: Price: ".$row->price."
";
16                                                            ". ;";
18 }
19 ?> A loop of lines will output it. During the output process, the value of the attribute of the row of data is obtained through the object operator "->".
The results of the sample run are as follows.



Copy code

The code is as follows:
Book title: Data Structure
Price: 20
Publication date: 2001 -01-01Book title: C languagePrice: 23Publication date: 1998-04-04Book title: PHP entry technology
Price: 22
Publication date: 2005-05-01






http://www.bkjia.com/PHPjc/327340.html

www.bkjia.com
true
http: //www.bkjia.com/PHPjc/327340.html

TechArticlemysql_fetch_object() is also used to obtain the query data result set, return the current row of data, and automatically slide to the next row. But unlike mysql_fetch_row() and mysql_fetch_array(), it returns...
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