What are the difference between mysqli_fetch_object and mysqli_fetch_array?

Last updated 2 years, 5 months ago | 1158 views 75     5

Tags:- PHP

PHP-MYSQL |  mysqli_fetch_object () VS  mysqli_fetch_array()

mysqli_fetch_object()  fetch a result row as an object where as mysqli_fetch_array()  fetch a result row as an associative array, a numeric array, or both.

 

 


 

mysqli_fetch_object()

The mysqli_fetch_object() function returns the current row of a result set, as an object.

Here is the example of mysqli_fetch_object() function.

<?php
// setup database
$con=mysqli_connect("localhost","user","password","db");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();die();
  }

$sql="SELECT * FROM Persons";
$result=mysqli_query($con,$sql);

// fetch data as an object
$row=mysqli_fetch_array($result);

// print row data
print_r($row);


mysqli_close($con);
?>

 


 

mysql_fetch_array()

The mysqli_fetch_array() function return the current row of result set as an associative array, a numeric array, or both.

Here is the example of mysqli_fetch_object() function.

<?php
// setup database
$con=mysqli_connect("localhost","user","password","db");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();die();
  }

$sql="SELECT * FROM Persons";
$result=mysqli_query($con,$sql);

// fetch data as an associative array, a numeric array, or both
$row=mysqli_fetch_array($result);

// print row data
print_r($row);


mysqli_close($con);
?>