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:

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

$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 with a username and password. We then execute a query using the mysqli_query() function and store the result in a variable. We check if there was a result using the $result variable. If there was a result, we call the mysqli_fetch_all() function to fetch all rows from the result set as an associative array. We then 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. Here is an example:

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

$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 call the mysqli_connect() function to connect to a MySQL database with a username and password. We then execute a query using the mysqli_query() function and store the result in a variable. We check if there was a result using the $result variable. If there was a result, we call the mysqli_fetch_all() function to fetch all rows from the result set as a numeric array. We then loop through the array and output the values of 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.

Practice Your Knowledge

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

Quiz Time: Test Your Skills!

Ready to challenge what you've learned? Dive into our interactive quizzes for a deeper understanding and a fun way to reinforce your knowledge.

Do you find this helpful?