Difference between mysql_fetch_object and mysql_fetch_array in PHP
Posted by Raj
difference between mysql_fetch_object and mysql_fetch_array in PHP
mysql_fetch_object :
mysql_fetch_object returns the results from database as objects.(fetches a result row as object)
$result->email
Example:
<?php
mysql_connect("host", "username", "pwd");
mysql_select_db("db");
$result = mysql_query("select * from employees");
while ($rows = mysql_fetch_object($result))
{
echo $empname=$row->name;
echo "<br>";
echo $empmail=$row->email;
}
?>
Output:
Rajesh
test@example.com
mysql_fetch_array:
Fetch a result row as an associative array, a numeric array, or both
$row[0],$row["name"];
Example:
<?php
mysql_connect("host", "username", "pwd");
mysql_select_db("db");
$result = mysql_query("select * from employees");
while ($rows = mysql_fetch_array($result))
{
echo $empname=$row[0];
echo "<br>";
echo $empmail=$row["email"];
}
?>
Output:
Rajesh
test@example.com
mysql_fetch_object :
mysql_fetch_object returns the results from database as objects.(fetches a result row as object)
$result->email
Example:
<?php
mysql_connect("host", "username", "pwd");
mysql_select_db("db");
$result = mysql_query("select * from employees");
while ($rows = mysql_fetch_object($result))
{
echo $empname=$row->name;
echo "<br>";
echo $empmail=$row->email;
}
?>
Output:
Rajesh
test@example.com
mysql_fetch_array:
Fetch a result row as an associative array, a numeric array, or both
$row[0],$row["name"];
Example:
<?php
mysql_connect("host", "username", "pwd");
mysql_select_db("db");
$result = mysql_query("select * from employees");
while ($rows = mysql_fetch_array($result))
{
echo $empname=$row[0];
echo "<br>";
echo $empmail=$row["email"];
}
?>
Output:
Rajesh
test@example.com
This entry was posted on October 4, 2009 at 12:14 pm, and is filed under
mysql_fetch_array,
mysql_fetch_object,
PHP,
PHP interview questions and answers
.You can leave a response, or trackback from your own site.