Using mysqli_fetch_assoc in PHP to Retrieve Rows as Associative Arrays

This article explains how to connect to a MySQL database with PHP, execute a SELECT query, and use the mysqli_fetch_assoc function to fetch rows as associative arrays, including code examples and best‑practice notes for handling multiple rows.

php Courses
php Courses
php Courses
Using mysqli_fetch_assoc in PHP to Retrieve Rows as Associative Arrays

In PHP, interacting with a database is a common task. When we execute a SELECT query and obtain a result set, we usually need to store the data in a PHP array for further processing.

PHP provides several functions for handling result sets, and one frequently used function is mysqli_fetch_assoc. This function retrieves one row from the result set as an associative array, making it easy to access data by column name.

Below is a complete example that demonstrates how to use mysqli_fetch_assoc to fetch a row from a result set:

<?php<br/>// Connect to the database<br/>$mysqli = new mysqli("localhost", "username", "password", "database");<br/><br/>// Check connection<br/>if ($mysqli->connect_errno) {<br/>    echo "Failed to connect to MySQL: " . $mysqli->connect_error;<br/>    exit();<br/>}<br/><br/>// Execute SELECT query<br/>$result = $mysqli->query("SELECT * FROM users");<br/><br/>// Verify that rows were returned<br/>if ($result->num_rows > 0) {<br/>    // Fetch one row as an associative array<br/>    $row = mysqli_fetch_assoc($result);<br/><br/>    // Output the values<br/>    echo "ID: " . $row['id'] . "<br>";<br/>    echo "Name: " . $row['name'] . "<br>";<br/>    echo "Age: " . $row['age'] . "<br>";<br/>} else {<br/>    echo "No data found.";<br/>}<br/><br/>// Close the connection<br/>$mysqli->close();<br/>?>

In the example, the query result is stored in $result. The call mysqli_fetch_assoc($result) returns a single row, which we store in $row. The $row array can then be accessed by field names such as 'id', 'name', and 'age'.

It is important to note that each call to mysqli_fetch_assoc returns the next row in the result set; when no more rows are available, it returns null. Therefore, to retrieve multiple rows you would typically place the call inside a loop.

Using mysqli_fetch_assoc simplifies handling database query results, improves code readability, and enhances maintainability.

In summary, the provided example shows how to use the PHP function mysqli_fetch_assoc to fetch a row from a result set as an associative array, which can be very helpful during development.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackenddatabaseTutorialMySQLifetch_assoc
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.