MySQLi Tutorial MySQLi References

mysqli_result fetch_assoc() Method



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

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, mysqli_fetch_row() may be used to fetch the numerically indexed array, or aliases may be used in the SQL query select list to give columns different names.

Syntax

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

//Procedural style
mysqli_fetch_assoc(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 associative array representing the fetched row, where each key in the array represents the name of one of the result set's columns, 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_assoc() 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);

//fetching associative array
while ($row = $result->fetch_assoc()) {
  printf("%s, %d\n", $row["Name"], $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

Example: Procedural style

The example below shows the usage of mysqli_fetch_assoc() 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);

//fetching associative array
while ($row = mysqli_fetch_assoc($result)) {
  printf("%s, %d\n", $row["Name"], $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

Example: comparison of mysqli_result iterator and mysqli_result::fetch_assoc() usage

mysqli_result can be iterated using foreach loop. The result set will always be iterated from the first row, regardless of the current position.

<?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();
}

$sql = "SELECT Name, Age FROM Employee ORDER BY Age";

//using iterators
$result = $mysqli->query($sql);
foreach ($result as $row) {
  printf("%s, %d\n", $row["Name"], $row["Age"]);
}

echo "\n==================\n";

//not using iterators
$result = $mysqli->query($sql);
while ($row = $result->fetch_assoc()) {
  printf("%s, %d\n", $row["Name"], $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

❮ MySQLi Functions Reference