Function Syntax
The following snippet shows the procedural syntax for the mysqli_fetch_array function:
mysqli_fetch_array(mysqli_result $result, int $mode = MYSQLI_BOTH): array|null|false
The function parameters are as shown:
- result – this parameter allows you to specify the mysql_result object returned by the mysqli_query(), mysqli_store_result(), mysqli_stmt_get_result(), or mysqli_use_result() functions.
- mode – the mode parameter defines the type of array used to store the resulting values. Accepted values include:
- MYSQLI_ASSOC – associative array.
- MYSQLI_NUM – numerical array.
- MYSQLI_BOTH – a combination of the associative and numerical array.
The function will then return the array with the fetched rows. If there are no fetched rows, the function returns null or false on failure.
Example Usage
Let us discuss an example of using the mysqli_fetch_array() function. Start by creating a sample database, table, and data as shown in the query below:
USE src;
DROP TABLE IF EXISTS stack_mapping;
CREATE TABLE stack_mapping(
id INT AUTO_INCREMENT PRIMARY KEY,
server_name VARCHAR(50) NOT NULL,
address VARCHAR(100) NOT NULL,
installed_version VARCHAR(50),
tool_id INT
);
INSERT INTO stack_mapping(server_name, address, installed_version, tool_id)
VALUES ('SQL Server', 'localhost:1433', '15.0', 1),
('Elasticsearch', 'localhost:9200', '8.4', 2),
('Redis', 'localhost:6379', '6.0', 3),
('PostgreSQL', 'localhost:5432', '14.5', 4),
('MySQL', 'localhost:3306', '8.0', NULL);
The resulting table is as shown:
The following example shows how to use PHP mysqli_fetch_array function to return the rows from the table above as an array.
Start by creating a PHP file:
Edit the file:
Add the code as shown:
$mysqli = mysqli_connect("localhost:3306", "root", "", "src");
if (mysqli_connect_errno()) {
die("Could not connect");
}
$query = "SELECT server_name, address FROM stack_mapping";
$result = mysqli_query($mysqli, $query);
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
printf("%s (%s)\n", $row["server_name"], $row["address"]);
}
mysqli_free_result($result);
?>
Finally, run the script with PHP:
This should return the rows as an associative array, also known as a dictionary in other programming languages.
NOTE: Note we are accessing the values of the dictionary using the row name (dictionary key).
An example output is as shown:
Elasticsearch (localhost:9200)
Redis (localhost:6379)
PostgreSQL (localhost:5432)
MySQL (localhost:3306)
To return the values as a numerical array, we can use the query:
$mysqli = mysqli_connect("localhost:3306", "root", "", "src");
if (mysqli_connect_errno()) {
die("Could not connect");
}
$query = "SELECT server_name, address FROM stack_mapping";
$result = mysqli_query($mysqli, $query);
while($row = mysqli_fetch_array($result, MYSQLI_NUM)) {
printf("%s (%s)\n", $row[0], $row[1]);
}
mysqli_free_result($result);
?>
Similarly, the code above should return the rows as:
Elasticsearch (localhost:9200)
Redis (localhost:6379)
PostgreSQL (localhost:5432)
MySQL (localhost:3306)
Finally, to get the results as both associative and numerical arrays.
$mysqli = mysqli_connect("localhost:3306", "root", "", "src");
if (mysqli_connect_errno()) {
die("Could not connect");
}
$query = "SELECT server_name, address FROM stack_mapping";
$result = mysqli_query($mysqli, $query);
while($row = mysqli_fetch_array($result, MYSQLI_BOTH)) {
printf("%s (%s)\n", $row[0], $row["address"]);
}
mysqli_free_result($result);
?>
Conclusion
In this article, you learned how to use the mysqli_fetch_array function in PHP to fetch the rows from a database as an array.