mysql_fetch_object vs MYSQL_FETCH_ARRAY php

What is the difference between mysql_fetch_object and MYSQL_FETCH_ARRAY

Answer:
The primary difference is that in fetch_object an object is returned while in FETCH_ARRAY an array is returned.

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

Check the example for MYSQL_FETCH_ARRAY Below:

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

$dbselected = mysql_select_db("testdb",$con);
$sql = "SELECT * from student WHERE Lastname='smith'";
$result = mysql_query($sql,$con);
print_r(mysql_fetch_array($result));

mysql_close($con);
?>

MYSQL FETCH OBJECT:
The mysql_fetch_object() function returns a row from a record set queas an object.

The Mysql_fetch_object 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.

Check the example below:

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

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

while ($row = mysql_fetch_object($result))
{
echo $row->Name. "
";
}

mysql_close($con);
?>

0 comments:

Post a Comment