PHP Function Reference

PHP mysqli_result fetch_array() Method



The PHP mysqli_result::fetch_array() / mysqli_fetch_array() function is used to fetch the next row of a result set as an associative, a numeric array, or both. Each subsequent call to this function will return the next row within the result set, or null if there are no more rows.

In addition to storing the data in the numeric indices of the result array, this function can also store the data in associative indices by using the field names of the result set as keys.

If two or more columns of the result have the same name, the last column will take precedence and overwrite any previous data. To access multiple columns with the same name, the numerically indexed version of the row must be used.

Syntax

//Object-oriented style
public mysqli_result::fetch_array(mode)

//Procedural style
mysqli_fetch_array(result, mode)

Parameters

result Required. For procedural style only: Specify a mysqli_result object returned by mysqli_query(), mysqli_store_result(), mysqli_use_result() or mysqli_stmt_get_result().
mode Optional. Indicates type of array produced from the current row data. The possible values are:
  • MYSQLI_ASSOC
  • MYSQLI_NUM
  • MYSQLI_BOTH
Default is MYSQLI_BOTH.

Return Value

Returns an array representing the fetched row, null if there are no more rows in the result set, or false on failure.

Example: Object-oriented style

The example below shows the usage of mysqli_result::fetch_array() method.

<?php
//establishing connection to the database
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno) {
  echo "Failed to connect to MySQL: ". $mysqli->connect_error;
  exit();
}

//getting query result from the database
$sql = "SELECT Name, Age FROM Employee ORDER BY Age";
$result = $mysqli->query($sql);

//associative array
$row = $result->array(MYSQLI_ASSOC);
printf("%s, %d\n", $row["Name"], $row["Age"]);

//numeric array
echo "\n";
$row = $result->array(MYSQLI_NUM);
printf("%s, (%d)\n", $row[0], $row[1]);

//associative and numeric array
echo "\n";
$row = $result->array(MYSQLI_BOTH);
printf("%s is %d years old.\n", $row[0], $row["Age"]);

//free result set
$result->free_result();

//closing the connection
$mysqli->close();
?>

The output of the above code will be similar to:

Marry, 23
Kim, 26
John, 27
Adam, 28

Marry (23)
Kim (26)
John (27)
Adam (28)

Marry is 23 years old.
Kim is 26 years old.
John is 27 years old.
Adam is 28 years old.

Example: Procedural style

The example below shows the usage of mysqli_fetch_array() function.

<?php
//establishing connection to the database
$mysqli = mysqli_connect("localhost", "user", "password", "database");
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: ". mysqli_connect_error();
  exit();
}

//getting query result from the database
$sql = "SELECT Name, Age FROM Employee ORDER BY Age";
$result = mysqli_query($mysqli, $sql);

//associative array
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
printf("%s, %d\n", $row["Name"], $row["Age"]);

//numeric array
echo "\n";
$row = mysqli_fetch_array($result, MYSQLI_NUM);
printf("%s, (%d)\n", $row[0], $row[1]);

//associative and numeric array
echo "\n";
$row = mysqli_fetch_array($result, MYSQLI_BOTH);
printf("%s is %d years old.\n", $row[0], $row["Age"]);

//free result set
mysqli_free_result($result);

//closing the connection
mysqli_close($mysqli);
?>

The output of the above code will be similar to:

Marry, 23
Kim, 26
John, 27
Adam, 28

Marry (23)
Kim (26)
John (27)
Adam (28)

Marry is 23 years old.
Kim is 26 years old.
John is 27 years old.
Adam is 28 years old.

❮ PHP MySQLi Reference