English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The mysqli_fetch_object() function retrieves the current row from the result set and returns it as an object.
Note: The field names returned by this function are case-sensitive.
A PHP result object (mysqli_result class) represents the MySQL result returned by a SELECT or DESCRIBE or EXPLAIN query.
mysqli_fetch_object()The function accepts a result object as a parameter, retrieves the content of the current row in the given result, and returns it as an object.
mysqli_fetch_object($result, [$class_name, $params]);
Serial number | Parameters and descriptions |
---|---|
1 | result (required) This is the identifier representing the result object. |
2 | class_name (optional) The name of the class to instantiate, set its properties, and return it. |
3 | params (optional) Specify an array of parameters to be passed to the constructor of the class_name object. |
The PHP mysqli_fetch_object() function returns an object (with string properties) that holds the current row of the result object. If there are no more rows, this function will return NULL.
This function was originally introduced in PHP version5introduced and can be used in all higher versions.
The following example demonstratesmysqli_fetch_object()Function usage (procedural style), returns all rows in the result set and then outputs the value of each field:
<?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("Create table.....\n"); mysqli_query($con, "INSERT INTO myplayers values(1, 'Sikhar', 'Dhawan', 'Delhi', 'India')"); mysqli_query($con, "INSERT INTO myplayers values(2, 'Jonathan', 'Trott', 'CapeTown', 'SouthAfrica')"); mysqli_query($con, "INSERT INTO myplayers values(3, 'Kumara', 'Sangakkara', 'Matale', 'Srilanka')"); print("Inserting record.....\n"); //Retrieve the content of the table $res = mysqli_query($con, "SELECT * FROM myplayers"); //Get all 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"); } //End statement mysqli_free_result($res); //Close connection mysqli_close($con); ?>
Output result
Create table..... Inserting record..... ID: 1 First_Name: Sikhar Last_Name: Dhawan Place_Of_Birth: Delhi Country: India ID: 2 First_Name: Jonathan Last_Name: Trott Place_Of_Birth: CapeTown Country: SouthAfrica ID: 3 First_Name: Kumara Last_Name: Sangakkara Place_Of_Birth: Matale Country: Srilanka
In the object-oriented style, the syntax of this function is$result-> fetch_object();.Here is an example of this function in an object-oriented style;
<?php //Establish connection $con = new mysqli("localhost", "root", "password", "mydb"); $con -> query("CREATE TABLE Test(Name VARCHAR(255), Age INT)"); $con -> query("insert into Test values('Raju', 25),('Rahman', 30),('Sarmista', 27); print("Create table.....\n"); $stmt = $con -> prepare("SELECT * FROM Test WHERE Name in(?, ?)"); $stmt -> bind_param("ss", $name1, $name2); $name1 = 'Raju'; $name2 = 'Rahman'; //Execute statement $stmt->execute(); //Search result $result = $stmt->get_result(); //Get all rows as an array while($obj = $result->fetch_object()){ print("Name: ".$obj->Name."\n"); print("Age: ".$obj->Age."\n"); } //End statement $stmt->close(); //Close connection $con->close(); ?>
Output result
Create table..... Name: Raju Age: 25 Name: Rahman Age: 30