Possible Duplicate:
mysql_fetch_array, mysql_fetch_assoc, mysql_fetch_object

I want a function which can return fields in the current row of a resultset into an associative array and moves the result pointer to the next row.

Confused between

mysql_fetch_row()
mysql_fetch_assoc()
mysql_fetch_array()

Which one should I use? Any help would be appreciated. Thank you.

share|improve this question
6  
Switch to MySQLi or PDO_MySQL – Dream Eater Jul 14 '12 at 1:05
2  
Why not read the documentation on php.net? – Evan Mulawski Jul 14 '12 at 1:07
Why you don't search first? stackoverflow.com/questions/1536813/… – Gabriel Santos Jul 14 '12 at 1:19
Please work on your accept-rate by accepting the answer you find most fitting.. – refp Jul 14 '12 at 7:13

marked as duplicate by kiamlaluno, Ben, Monolo, Jason Sturges, random Jul 15 '12 at 3:57

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

4 Answers

up vote 2 down vote accepted

Note: The use of the mysql_* functions are considered deprecated and you should instead use something that offers better security and more functionality, such as MySQLi or PDO.

What is it?

You are looking for mysql_fetch_assoc, as the name implies it will return an associative array (with the column names as keys and the values as the row values).


What will the different functions return?

All of the mentioned functions will return an array, the differences between them is what values that are being used as keys in the returned object.

  • mysql_fetch_row

    This function will return a row where the values will come in the order as they are defined in the SQL query, and the keys will span from 0 to one less than the number of columns selected.

  • mysql_fetch_assoc

    This function will return a row as an associative array where the column names will be the keys storing corresponding value.

  • mysql_fetch_array

    This function will actually return an array with both the contents of mysql_fetch_rowand mysql_fetch_assoc merged into one. It will both have numeric and string keys which will let you access your data in whatever way you'd find easiest.

    It is recommended to use either _assoc or _row though.

share|improve this answer
Note, all of these functions have nearly-identically-named counterparts in the mysqli API. So you won't be losing anything or have much to relearn much if you switch. – cHao Jul 14 '12 at 1:45

mysql_fetch_assoc()

Say your table has two columns id and name. You can use the following snippet -

while ($row = mysql_fetch_assoc($result)) {
    echo $row["id"];
    echo $row["name"];
}
share|improve this answer

mysql_fetch_assoc when you are manually referring to the fields.

mysql_fetch_row when you are using it as part of a list($a,$b,$c...) = ...

Never mysql_fetch_array.

share|improve this answer

As Nerd_Herd said, and I know it isn't the answer to your question, but since it looks like you are just starting out, don't use the old MySQL way of doing this. Look at PDO or MySQLi.

share|improve this answer
Considering the mysql extension is all but deprecated at this point, it's worth looking at mysqli (which works a lot like mysql). The question's still there even if you switch, though, as there's a mysqli_fetch_assoc, mysqli_fetch_array, mysqli_fetch_row, and even a mysqli_fetch_object, that work much like their mysql_ counterparts. – cHao Jul 14 '12 at 1:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.