W3Schools.com

PHP mysql_fetch_object() Function


PHP MySQL Reference Complete PHP MySQL Reference

Definition and Usage

The mysql_fetch_object() function returns a row from a recordset as an object.

This function gets a row from the mysql_query() function and returns an object on success, or FALSE on failure or when there are no more rows.

Syntax

mysql_fetch_object(data)

Parameter Description
data Required. Specifies which data pointer to use. The data pointer is the result from the mysql_query() function


Tips and Notes

Note: Each subsequent call to mysql_fetch_object() returns the next row in the recordset.


Example

<?php
$con = mysql_connect("localhost", "peter", "abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

$db_selected = mysql_select_db("test_db",$con);
$sql = "SELECT * from Person";
$result = mysql_query($sql,$con);

while ($row = mysql_fetch_object($result))
  {
  echo $row->FirstName . "<br />";
  }

mysql_close($con);
?>

The output of the code above could be:

Kai Jim
Ståle
Hege


PHP MySQL Reference Complete PHP MySQL Reference
WEB HOSTING
Best Web Hosting
PHP MySQL Hosting
Best Hosting Coupons
UK Reseller Hosting
Cloud Hosting
Top Web Hosting
$7.95/mo SEO Hosting
Premium Website Design
WEB BUILDING
XML Editor - Free Trial!
FREE Website BUILDER
Free Website Templates Free CSS Templates
Make Your Own Website
W3SCHOOLS EXAMS
Get Certified in:
HTML, CSS, JavaScript, XML, PHP, and ASP
W3SCHOOLS BOOKS
New Books:
HTML, CSS
JavaScript, and Ajax
STATISTICS
Browser Statistics
Browser OS
Browser Display
SHARE THIS PAGE