mysql_fetch_object ()Also used to get the query data result set, return the current row data, and automatically slide down the line. But with
mysql_fetch_row () and mysql_fetch_array ()The difference is that it returns an object whose property collection is the collection of properties for the data, whereas the value on the property is the value on that property of the current row in the database. The function is defined as follows.
Copy Code code as follows:
Object Mysql_fetch_object (int result, int [result_type])
parameter description with mysql_fetch_array ().
The return value is as follows.
Success:An object that corresponds to the property name in the result set, and the value of the property is the corresponding property value in the result set.
failed:False
The following is an example of using Mysql_fetch_object (): Query the book information in the datasheet computers.
Copy Code code as follows:
1 <!---use Mysql_fetch_object () to get Data: mysql_fetch_object.php--->
2 <?php
3//Connect and select to 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//echo $query. " <br> ";
$result =mysql_query ($query, $connection);
//mysql_fetch_object () Get the data and output
while ($row =mysql_fetch_object ($result))
13 {
echo "title:" $row->name. " <br> ";
echo "Price:". $row->price. " <br> ";
echo "Published date:" $row->publish_date. " <br> ";
echo "<br>";
18}
?>
Example is used on line 12th
mysql_fetch_object () Gets the data for the current row, and outputs it by looping through the 12~18 line. In the output process, the object operator "->" gets the value of the row data on its properties.
The results of the sample run are as follows.
Copy Code code as follows:
Title: Data structure
Price: 20
Release Date: 2001-01-01
Title: C language
Price: 23
Release Date: 1998-04-04
Book title: Introduction to PHP Technology
Price: 22
Release Date: 2005-05-01