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

Introduction to the mysqli_fetch_assoc() function

The mysqli_fetch_assoc() function is a built-in function in PHP that is used to fetch a row from a MySQLi result set as an associative array. This function is useful when you need to fetch a single row from a MySQLi query and store it in an array for further processing.

How to use the mysqli_fetch_assoc() function

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

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

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

if ($result) {
    $row = mysqli_fetch_assoc($result);
    echo $row['column1'] . " - " . $row['column2'];


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 with a WHERE clause to select a single row from the table. We store the result in a variable and check if there was a result using the $result variable. If there was a result, we call the mysqli_fetch_assoc() function to fetch a row from the result set as an associative array. We then output the values of two columns for the row.


In conclusion, the mysqli_fetch_assoc() function is a useful tool for fetching a single row from a MySQLi result set and storing it in an associative array for further processing. By understanding how to use the function, you can take advantage of this feature to create powerful and flexible MySQLi queries.

Practice Your Knowledge

What does the fetch_assoc() function in PHP do?

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?