PHP Tutorial - PHP mysqli_fetch_object() Function






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

ParameterIs RequiredDescription
resultRequired.Result set returned by mysqli_query(), mysqli_store_result() or mysqli_use_result()
classnameOptional.Name of the class to instantiate
paramsOptional.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/* 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);
?>