Current Location: Home> Function Categories> mysql_fetch_object

mysql_fetch_object

Get a row from the result set as an object.
Name:mysql_fetch_object
Category:Uncategorized
Programming Language:php
One-line Description:Get the next row from the result set as an object

Function name: mysql_fetch_object()

Applicable versions: PHP 4, PHP 5 (deprecated for PHP 7.0.0 and removed in PHP 7.2.0)

Usage: mysql_fetch_object(resource $result [, string $class_name = "stdClass" [, array $params]])

Description: The mysql_fetch_object() function is used to obtain the next row from the result set as an object.

parameter:

  • $result: Required, represents the resource of the result set.
  • $class_name (optional): represents the name of the class to be instantiated. The default is "stdClass", which means using standard anonymous objects.
  • $params (optional): an array of parameters passed to the constructor.

Return value:

  • If successful, return an object.
  • If there are no more rows, false is returned.

Example:

  1. Use the default "stdClass" class name:
 $result = mysql_query("SELECT * FROM users"); while ($row = mysql_fetch_object($result)) { echo $row->name . " - " . $row->email . "<br>"; }
  1. Use custom class names and constructor parameters:
 class User { public $name; public $email; public function __construct($name, $email) { $this->name = $name; $this->email = $email; } } $result = mysql_query("SELECT name, email FROM users"); while ($row = mysql_fetch_object($result, "User", ["John Doe", "john@example.com"])) { echo $row->name . " - " . $row->email . "<br>"; }

Notes:

  • mysql_fetch_object() function has been deprecated and is no longer recommended. It is recommended to use mysqli_fetch_object() or PDO::fetchObject() instead.
  • In PHP 7, mysql_fetch_object() has been removed and cannot be used.
Similar Functions
Popular Articles