What are the difference between mysqli_fetch_assoc and mysqli_fetch_array?

1577 Views

What are the difference between mysqli_fetch_assoc and mysqli_fetch_array?

PHP-MYSQL |  mysqli_fetch_assoc() VS  mysqli_fetch_array()

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

 

 


 

mysqli_fetch_assoc()

The mysqli_fetch_assoc() function returns the current row of the result set as an associative array.

Here is an example of mysqli_fetch_assoc() 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
$row=mysqli_fetch_assoc($result);

// print row data
print_r($row);


mysqli_close($con);
?>

 


 

mysqli_fetch_array()

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

Here is an example of mysqli_fetch_array() 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);
?>

 

Tags:-
PHP
PHP-MySQL