PHP mysqli_fetch_array() Function

PHP mysqli_fetch_array() Function is used to fetch rows as a numeric array and as an associative or both array from the database.

Associative arrays are the arrays where the indexes are the names of the individual columns of the table. Numeric arrays are arrays where indexes are numbers with 0 representing the first column and n-1 representing the last column of an n-column table.

PHP Version : PHP 5, PHP 7

Syntax for mysqli_fetch_array():

According to Procedural,

mysqli_fetch_array($resultset,$mode);

According to Object oriented,

$mysqli_result -> fetch_array($mode);

Parameter,

$resultset : Required. It is a result set identifier returned by mysqli_query(), mysqli_store_result() or mysqli_use_result().

$mode : Optional. Specifies what type of array that should be produced. It can have following three values:

  • MYSQLI_ASSOC - This constant behave like mysqli_fetch_assoc() function.
  • MYSQLI_NUM - This constant behave like mysqli_fetch_row() function.
  • MYSQLI_BOTH - It is default. This constant behave like numeric array and associative array.

Return values for mysqli_fetch_array():

It returns an array of values that corresponds to the fetched row or null if there are no more rows in result set.

Example for mysqli_fetch_array():

Let's see below example to understand php mysqli_fetch_array() Function in details.

<?php
$connection = mysqli_connect("localhost","root","","employee");

if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL server : " . mysqli_connect_error();
  exit();
}

$resultset = mysqli_query($connection , "SELECT name, phone ,salary FROM employee");

 echo " <br> <b> Name  Phone  Salary </b>";

while($row = mysqli_fetch_array($resultset , MYSQLI_ASSOC))
{

       echo " <br> ".$row['name']. "  ".$row['phone']."  ".$row['salary'];

}

mysqli_free_result($result);

mysqli_close($connection );
?>

Output :

Name   Phone          Salary
Rohit    98777*****    5000
Aliya    78657*****    7000
Jayesh 73452*****    6000

 

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

20028