Skip to content

fetch_all

In this article, we will focus on the mysqli_fetch_all() function in PHP, which is used to fetch all rows from a MySQLi result set as an associative or numeric array. We will provide you with an overview of the function, how it works, and examples of its use.

Introduction to the mysqli_fetch_all() function

The mysqli_fetch_all() function is a built-in function in PHP that is used to fetch all rows from a MySQLi result set as an associative or numeric array. This function is useful when you need to fetch all rows from a MySQLi query and store them in an array for further processing.

How to use the mysqli_fetch_all() function

Using the mysqli_fetch_all() function is very simple. You just need to call the function on a valid MySQLi result set. Here is an example:

How to use the mysqli_fetch_all() function?

php
<?php
$mysqli = mysqli_connect("localhost", "username", "password", "database");

if (!$mysqli) {
    die("Connection failed: " . mysqli_connect_error());
}

$query = "SELECT * FROM my_table";
$result = mysqli_query($mysqli, $query);

if ($result) {
    $rows = mysqli_fetch_all($result, MYSQLI_ASSOC);
    foreach ($rows as $row) {
        echo $row['column1'] . " - " . $row['column2'] . "\n";
    }
}

mysqli_close($mysqli);
?>

In this example, we call the mysqli_connect() function to connect to a MySQL database. We first verify the connection succeeded to prevent fatal errors. We then execute a query using the mysqli_query() function and store the result in a variable. If the query returns a result, we call mysqli_fetch_all() to fetch all rows as an associative array. Finally, we loop through the array and output the values of two columns for each row.

Advanced usage

The mysqli_fetch_all() function can also be used in more advanced scenarios. For example, you can use the function to fetch rows as a numeric array instead of an associative array. You can also use the MYSQLI_BOTH constant to retrieve rows with both associative and numeric indices. Here is an example:

Advanced usage of PHP mysqli_fetch_all()

php
<?php
$mysqli = mysqli_connect("localhost", "username", "password", "database");

if (!$mysqli) {
    die("Connection failed: " . mysqli_connect_error());
}

$query = "SELECT * FROM my_table";
$result = mysqli_query($mysqli, $query);

if ($result) {
    $rows = mysqli_fetch_all($result, MYSQLI_NUM);
    foreach ($rows as $row) {
        echo $row[0] . " - " . $row[1] . "\n";
    }
}

mysqli_close($mysqli);
?>

In this example, we establish a secure connection and execute a query. If the result is valid, we call mysqli_fetch_all() with the MYSQLI_NUM constant to fetch rows as a numeric array. We then loop through the array and output the values of the first two columns for each row.

Conclusion

In conclusion, the mysqli_fetch_all() function is a useful tool for fetching all rows from a MySQLi result set and storing them in an array for further processing. By understanding how to use the function and its advanced usage scenarios, you can take advantage of this feature to create powerful and flexible MySQLi queries. Note that the MySQLi extension must be enabled in your php.ini, and mysqli_fetch_all() is available in PHP 5.3 and later.

Practice

What does the fetchAll() function in PHP do according to the site w3docs.com?

Do you find this helpful?

Dual-run preview — compare with live Symfony routes.