The mysqli_fetch_object() function returns the current row of a result set, as an object.
PHP mysqli_fetch_object() Function has the following syntax.
mysqli_fetch_object(result,classname,params);
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 |
It returns an object with string properties for the fetched row. NULL if there are no more rows in the result set.
The following table returns the current row of a result set, then print each field's value.
<?php/* w ww. j a va 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);
?>