Fueling Your Coding Mojo

Buckle up, fellow PHP enthusiast! We're loading up the rocket fuel for your coding adventures...

Popular Searches:

PHP fetch_assoc() function (with example)

Hey everyone,

I hope you're all doing well. I'm fairly new to PHP and I recently came across the fetch_assoc() function in PHP. I've been trying to understand how it works and how it can be used effectively in my code. From what I've understood so far, fetch_assoc() is a function used in combination with the MySQLi extension in PHP to fetch a result row as an associative array.

But I'm still a bit confused about its usage and how it exactly works. Could someone please explain it to me in a simpler way, maybe with some examples? I would really appreciate it.

Thank you so much in advance! Looking forward to your responses.

All Replies


Hey there,

I see a lot has already been explained about the fetch_assoc() function, but I'd like to share my personal experience with it as well. I hope it provides you with some additional insights.

In a recent project, I had to retrieve data from a MySQL database and wanted to store it in an easily accessible format. This is where fetch_assoc() came to my rescue. By using this function, I could fetch each row as an associative array, which made manipulating the data much more convenient.

One particular scenario where I found fetch_assoc() immensely helpful was when I needed to display user information on a webpage. Instead of juggling with numerical indices, I could use the column names as keys to access the values, which made my code more readable and maintainable.

Here's a snippet from my project to give you a better idea:

// Assuming the connection to the database has been established
$query = "SELECT * FROM users";
$result = $mysqli->query($query);

// Fetching data and rendering it on the webpage
while ($row = $result->fetch_assoc()) {
echo "User ID: " . $row['id'] . "<br>";
echo "Name: " . $row['name'] . "<br>";
echo "Email: " . $row['email'] . "<br><br>";

By using fetch_assoc(), I could easily access specific user attributes like ID, name, and email without worrying about their index positions in the result set. Plus, it made my code more self-explanatory, as column names directly reflected the data I was accessing.

Overall, fetch_assoc() is a handy function when dealing with MySQL data because it allows for efficient and readable code. If you have any more specific questions or need further clarification, feel free to ask. Happy coding!


Hey there,

I can definitely help shed some light on the fetch_assoc() function in PHP. I've used it quite a bit in my projects, so hopefully, my experience can be of assistance to you.

The fetch_assoc() function is commonly used when working with MySQL databases in PHP. It's used to fetch a single row of data from a result set as an associative array. This means that instead of accessing the data using numeric indices, you can access it using column names as keys in the array.

Here's a simple example to illustrate how it works:

// Assuming you already have a connection to the database
$query = "SELECT id, name, email FROM users";
$result = $mysqli->query($query);

// Fetching data as associative array
while ($row = $result->fetch_assoc()) {
echo "User ID: " . $row["id"] . "<br>";
echo "Name: " . $row["name"] . "<br>";
echo "Email: " . $row["email"] . "<br>";

In this example, the fetch_assoc() function fetches each row from the result set one by one, and each row is then accessed as an associative array using column names like `$row["id"]`, `$row["name"]`, and `$row["email"]`.

This function proves to be very useful when you want to access specific values from a result set and perform operations on them. It makes your code more readable and easy to understand, especially when dealing with multiple columns in a query result.

I hope this explanation and the example helps you out! If you have any more questions or need further clarifications, feel free to ask.

New to LearnPHP.org Community?

Join the community