MySQLi Tutorial MySQLi References

mysqli_result fetch_row() Method



The mysqli_result::fetch_row() / mysqli_fetch_row() function fetches one row of data from the result set and returns it as an enumerated array, where each column is stored in an array offset starting from 0 (zero). Each subsequent call to this function will return the next row within the result set, or null if there are no more rows.

Syntax

//Object-oriented style
public mysqli_result::fetch_row()

//Procedural style
mysqli_fetch_row(result)

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().

Return Value

Returns an enumerated 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_row() 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 EmpID, Name, Age FROM Employee ORDER BY Age";
$result = $mysqli->query($sql);

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

//processing the data retrieved from the 
//database - fetching object array
while($row = $result->fetch_row()) {
  printf("%s, %d\n", $row[1], $row[2]);
}
?>

The output of the above code will be similar to:

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

Example: Procedural style

The example below shows the usage of mysqli_fetch_row() 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 EmpID, Name, Age FROM Employee ORDER BY Age";
$result = mysqli_query($mysqli, $sql);

//closing the connection
mysqli_close($mysqli);

//processing the data retrieved from the 
//database - fetching object array
while($row = mysqli_fetch_row($result)) {
  printf("%s, %d\n", $row[1], $row[2]);
}
?>

The output of the above code will be similar to:

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

❮ MySQLi Functions Reference