PHP MySQL Interview Questions & Answers

PHP MySQL Interview Questions & Answers

What is the difference between mysqli_fetch_array() and mysqli_fetch_object()?

Both PHP functions are used to fetch data from MYSQL database, but there is a difference between mysqli_fetch_array() and mysqli_fetch_object() output format.

With the help of mysqli_fetch_array() function, we can fetch a query’s result data row as an array.

Example of mysqli_fetch_array() Function:

<?php
$conn=mysqli_connect("localhost","username","password","db");
$query="SELECT students, marks FROM class";
$result=mysqli_query($conn,$query);
while ($arr=mysqli_fetch_array($result))
{
    echo $arr['students']." ".$arr['marks'];
    echo "<br />";
}
mysqli_close($conn);
?>

While mysqli_fetch_object() function, we can fetch a query’s result data row as an object.

Example of mysqli_fetch_object() Function:

<?php
$conn=mysqli_connect("localhost","username","password","db");
$query="SELECT students, marks FROM class";
$result=mysqli_query($conn,$query);
while ($obj=mysqli_fetch_object($result))
{
    echo $obj->students." ".$obj->marks;
    echo "<br />";
}
mysqli_close($conn);
?>