In depth analysis USES mysql_fetch_object of to return query results as objects

  • 2020-06-03 06:10:53
  • OfStack

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

object mysql_fetch_object( int result, int [result_type])

Parameter specification is the same as mysql_fetch_array().
The return value is as follows.
Success: 1 object whose property name corresponds to the property name in the result set, whose value is the corresponding property value in the result set.
Failure: false.
Here is an example of using mysql_fetch_object() : Query the data table Computers for book information.

1    <!--- use mysql_fetch_object() To obtain data: mysql_fetch_object.php--->
2    <?php
3        // Connect and select to 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        //echo $query."<br>";
10       $result=mysql_query($query,$connection);
11       //mysql_fetch_object() Get the data and output it 
12       while($row=mysql_fetch_object($result))
13       {
14            echo " Title:     ".$row->name."<br>";
15            echo " Price:     ".$row->price."<br>";
16            echo " Publication Date:     ".$row->publish_date."<br>";
17            echo "<br>";
18       }
19    ?>   

The example USES mysql_fetch_object() on line 12 to get the data for the current row and output it through a loop between lines 12 and 18. During the output, through the object operator "- > Gets the value of the row's data on its property.
The results of the sample run are as follows.

 Title:   The data structure 
 Price:  20
 Publication Date:  2001-01-01
 Title:  C language 
 Price:  23
 Publication Date:  1998-04-04
 Title:  PHP Introduction to technology 
 Price:  22
 Publication Date:  2005-05-01


Related articles: