Summary: in this tutorial, you’ll learn how to use the fetchObject() method to fetch the next row from a result set and returns it as an object.
Introduction to the fetchObject() method #
Suppose that you have a pulishers table:
CREATE TABLE IF NOT EXISTS publishers (
publisher_id INT AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
PRIMARY KEY (publisher_id)
);and a Publisher class:
<?php
class Publisher
{
private $publisher_id;
private $name;
}To fetch each row from the publishers table and returns it as a Publisher object, you use the fetchObject() method:
public function fetchObject(
string|null $class = "stdClass",
array $constructorArgs = []
): object|falseThe fetchObject() is a method of the PDOStatement class. It fetches the next row from the result set associated with a PDOStatement object and returns an object of a class. If the fetchObject() method fails, it’ll return false instead.
The fetchObject() method has two parameters:
$classspecifies the class of the object to return. If you omit it, thefetchObject()method will return an instance of thestdClassclass.constructorArgsis an array that specifies the arguments passed to the constructor of the$class.
The fetchObject() maps the columns of the row with the properties of the object with the following rules:
- First, assign the column value to the property with the same name.
- Second, call the
__set()magic method if a column doesn’t have a property in the class with the same name. If the classs has no__set()magic method, create a public property with the value derived from the column.
PHP fetchobject() method example #
The following example shows how to use the fetchObject() method to fetch a row from the publishers table and returns a Publisher object:
<?php
class Publisher
{
private $publisher_id;
private $name;
}
// connect to the database
$pdo = require 'connect.php';
// execute the query
$sql = 'SELECT publisher_id, name
FROM publishers
WHERE publisher_id=:publisher_id';
$statement = $pdo->prepare($sql);
$statement->execute([':publisher_id' => 1]);
// fetch the row into the Publisher object
$publisher = $statement->fetchObject('Publisher');
var_dump($publisher);Output:
object(Publisher)#3 (2) {
["publisher_id":"Publisher":private]=> string(1) "1"
["name":"Publisher":private]=> string(21) "McGraw-Hill Education"
}How it works.
First, define a Publisher class with two properties publisher_id and name:
class Publisher
{
private $publisher_id;
private $name;
}Second, connect to the bookdb database using the connect.php object:
$pdo = require 'connect.php';Third, execute a prepared statement that selects a publisher with id 1 from the publishers table:
// execute the query
$sql = 'SELECT publisher_id, name
FROM publishers
WHERE publisher_id=:publisher_id';
$statement = $pdo->prepare($sql);
$statement->execute([':publisher_id' => 1]);Finally, return a Publisher object:
$publisher = $statement->fetchObject('Publisher');Note that if the Publisher class has a namespace, you need to pass a fully qualified class name. For example:
<?php
namespace phptutorial;
class Publisher
{
private $publisher_id;
private $name;
}
// connect to the database
$pdo = require 'connect.php';
// execute the query
$sql = 'SELECT publisher_id, name
FROM publishers
WHERE publisher_id=:publisher_id';
$statement = $pdo->prepare($sql);
$statement->execute([':publisher_id' => 1]);
// fetch the row into the Publisher object
$publisher = $statement->fetchObject('phptutorial\Publisher');
var_dump($publisher);Note the first line that defines the namespace phptutorial:
namespace phptutorial;And the line that uses the fetchObject() method:
$publisher = $statement->fetchObject('phptutorial\Publisher');Summary #
- Use the
fetchObject()method to fetch the next row from a result set and returns it as an object of a class.
Thank you for your feedback!