PHP Tutorial





Español Français 中文 Deutsch Portuguese Japanese nederlands
   
 
PHP Topics
Introduction Introduction
Syntax Syntax
Data Types Data Types
Operators Operators
Control Structures Control Structures
Functions Functions
Pre-defined Function Pre-defined Function
Calendar Functions Calendar Functions
Date and Time Date and Time
Array Functions Array Functions
Array List Array Functions List1
Array Function List Array Functions List2
Math Functions Math Functions
PHP MYSQL Functions PHP Mysql Functions
File Handling File Handling
Error Handling Error Handling
DB Size DB Size
PHP Mail PHP Mail
String Tokens String Tokens
String Functions String Functions
String Functions List String Functions List1
String Functions List2 String Functions List2
Session Functions Session Functions
Cookies Functions Cookies Functions
Form Variables Form Variables
Running PHP from JS Running PHP from JS
Array To JS Array To JS
JS Array Array to PHP
Encryption Encryption
Common Header Common Header
Forums Ask Your Doubts
Scraps More about PHP
Feedback Feedback
 




mysql_fetch_object() Function in PHP


Tutorials »Php »

Topic

What is mysql_fetch_object() function in PHP?
How does mysql_fetch_object() works?




Explanation

This mysql_fetch_object() function fetches a query result row and return as an object.

Syntax
object mysql_fetch_object ( resource result)

Returns an object with properties that correspond to the fetched row, or FALSE if there are no more rows.

mysql_fetch_object() is similar to mysql_fetch_array(), with one difference - an object is returned, instead of an array. Indirectly, that means that you can only access the data by the field names, and not by their offsets.

mysql_fetch_object() function retrieves a row of data from a result returned by mysql_db_query() or mysql_query() and return as an object, you can retrieve the field value with the field name instead of field index. Each subsequent call to mysql_fetch_object() returns the next row in the recordset.

The data is returned as an object.

Example:


<?php

//Attempt to connect to the default database server
$link = mysql_connect("mysql_host", "mysql_user", "mysql_password")
or die ("Could not connect");

//select database
if (!mysql_select_db("my_database", $link)) {
echo " ERROR NO: " . mysql_errno($link) . "\n";
}

// Simple Select query
$query = "SELECT * FROM my_table";

$result = mysql_query($query);

//Fetches row of result set as an object
while ($row = mysql_fetch_object($result)) {
echo $row->user_id;
echo $row->fullname;
}

mysql_free_result($result);
?>


Result:

The output of above code is given below
1 jim
2 kiet
3 john


See also:





Other Links

web hosting