Deep analysis uses mysql_fetch_object () to return query results as an object _php tips

Source: Internet
Author: User
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

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.