PHP mysqli_fetch_object() Function
In this chapter you will learn:
- Definition for PHP mysqli_fetch_object() Function
- Syntax for PHP mysqli_fetch_object() Function
- Parameter for PHP mysqli_fetch_object() Function
- Return for PHP mysqli_fetch_object() Function
- Example - returns the current row of a result set, then print each field's value
Definition
The mysqli_fetch_object() function returns the current row of a result set, as an object.
Syntax
PHP mysqli_fetch_object() Function has the following syntax.
mysqli_fetch_object(result,classname,params);
Parameter
Parameter | Is Required | Description |
---|---|---|
result | Required. | Result set returned by mysqli_query(), mysqli_store_result() or mysqli_use_result() |
classname | Optional. | Name of the class to instantiate |
params | Optional. | Array of parameters to pass to the constructor for classname objects |
Return
It returns an object with string properties for the fetched row. NULL if there are no more rows in the result set.
Example
The following table returns the current row of a result set, then print each field's value.
<?php/*from j a v a 2s . c o m*/
$con=mysqli_connect("localhost","my_user","my_password","my_db");
if (mysqli_connect_errno($con)){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="SELECT name FROM emp";
if ($result=mysqli_query($con,$sql)){
while ($obj=mysqli_fetch_object($result)){
print $obj->name;
print "\n";
}
mysqli_free_result($result);
}
mysqli_close($con);
?>
Next chapter...
What you will learn in the next chapter:
- Definition for PHP mysqli_fetch_row() Function
- Syntax for PHP mysqli_fetch_row() Function
- Parameter for PHP mysqli_fetch_row() Function
- Return for PHP mysqli_fetch_row() Function
- Example - fetches one row from a result-set and returns it as an enumerated array
Home » PHP Tutorial » PHP MySQLi Functions