PHP | mysqli_fetch_object() Function

PHP mysqli_fetch_object() Function


Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP mysqli_fetch_object() Function.

The built-in mysqli_fetch_object() function accepts a result object as a parameter and, retrieves the contents of current row in the given result and returns them as an object.

Syntax

Following below is the syntax to use this function -

mysqli_fetch_object($result, [$class_name, $params]);


Parameter Details

Sr.NoParameter & Description
1

result(Mandatory)

This is an identifier representing a result object.

2

class_name(Optional)

The name of the class to instantiate, set the properties of and return.

3

params(Optional)

An array representing the optional parameters.


Return Value

This built-in PHP function returns an object (with string properties) which holds the current row of the result object. This PHP function returns NULL if there are no more rows.

PHP Version

This PHP function was first introduced in PHP version 5 and works in all the later versions.

Example1

The following below is an example which demonstrates the usage of PHP mysqli_fetch_object() function (in a procedural style) -

<?php
   $con = mysqli_connect("localhost", "root", "password", "mydb");

   mysqli_query($con, "CREATE TABLE myplayers(ID INT, First_Name VARCHAR(255), Last_Name VARCHAR(255), Place_Of_Birth VARCHAR(255), Country VARCHAR(255))");
   print("Table Created.....\n");
   mysqli_query($con, "INSERT INTO myplayers values(1, 'Kennedy', 'Nkpara', 'PortHarcourt', 'Nigeria')");
   mysqli_query($con, "INSERT INTO myplayers values(2, 'Jonathan', 'Trott', 'CapeTown', 'SouthAfrica')");
   mysqli_query($con, "INSERT INTO myplayers values(3, 'Queen', 'Douglas', 'Texas', 'UnitedStates')");
   print("Record Inserted.....\n");

   //Retrieving the contents of the table
   $res = mysqli_query($con, "SELECT * FROM myplayers");

   //Fetching all the rows as objects
   while($obj = mysqli_fetch_object($res)){
      print("ID: ".$obj->ID."\n");
      print("First_Name: ".$obj->First_Name."\n");
      print("Last_Name: ".$obj->Last_Name."\n");
      print("Place_Of_Birth: ".$obj->Place_Of_Birth."\n");
      print("Country: ".$obj->Country."\n");
   }
   //Closing the statement
   mysqli_free_result($res);

   //Closing the connection
   mysqli_close($con);
?>

Output

When the above code is executed, it will produce the following result -

Table Created.....
Record Inserted.....
ID: 1
First_Name: Kennedy
Last_Name: Nkpara
Place_Of_Birth: PortHarcourt
Country: Nigeria
ID: 2
First_Name: Jonathan
Last_Name: Trott
Place_Of_Birth: CapeTown
Country: SouthAfrica
ID: 3
First_Name: Queen
Last_Name: Douglas
Place_Of_Birth: Texas
Country: UnitedStates

Example2

In an object oriented style the syntax of this function is $result->fetch_object(); Following is the example of this function in an object oriented style -

<?php
   //Creating a connection
   $con = new mysqli("localhost", "root", "password", "mydb");

   $con -> query("CREATE TABLE Test(Name VARCHAR(255), Age INT)");
   $con -> query("insert into Test values('Kennedy', 27),('Paul', 30),('Justice', 28)");
   print("Table Created.....\n");

   $stmt = $con -> prepare( "SELECT * FROM Test WHERE Name in(?, ?)");
   $stmt -> bind_param("ss", $name1, $name2);
   $name1 = 'Kennedy';
   $name2 = 'Paul';

   //Executing the statement
   $stmt->execute();

   //Retrieving the result
   $result = $stmt->get_result();

   //Fetching all the rows as arrays
   while($obj = $result->fetch_object()){	
      print("Name: ".$obj->Name."\n");
      print("Age: ".$obj->Age."\n");
   }
   //Closing the statement
   $stmt->close();

   //Closing the connection
   $con->close();
?>

Output

When the above code is executed, it will produce the following result -

Table Created.....
Name: Kennedy
Age: 27
Name: Paul
Age: 30


Alright guys! This is where we are going to be rounding up for this tutorial post. In our next tutorial, we are going to be discussing about the mysqli_fetch_row() Function in PHP.

Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Do follow us on our various social media handles available and also subscribe to our newsletter to get our tutorial posts delivered directly to your emails.

Thanks for reading and bye for now.