mysql_fetch_object() and mysql_fetch_array() Functions

Q

What is the difference between mysql_fetch_object() and mysql_fetch_array() functions in PHP?

✍: FYIcenter

A

mysql_fetch_object() fetches the current row of data from the query result associated with the specified result identifier. The row is returned as an object. See the example code:

<?php
$result = mysql_query("select * from fyiUser");
while ($row = mysql_fetch_object($result)) {
   echo $row->user_id;
   echo $row->fullname;
}
mysql_free_result($result);
?>

mysql_fetch_array() fetches the current row of data from the query result associated with the specified result identifier. The row is returned as an array with 3 options: an associative array, a numeric array, or both. The default behavior of mysql_fetch_array() is to return the row as an associative array and a numeric array. See the example code:

<?php
$result = mysql_query("select * from fyiUser");
while ($row = mysql_fetch_array($result)) {
   printf ("ID: %s  Name: %s", $row[0], $row["name"]);
}
mysql_free_result($result);
?>

2007-02-27, 6911👍, 0💬