25

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.

Community
  • 1
  • 1
user1479431
  • 369
  • 1
  • 5
  • 10

3 Answers3

52

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.

cssyphus
  • 37,875
  • 18
  • 96
  • 111
Filip Roséen - refp
  • 62,493
  • 20
  • 150
  • 196
  • 1
    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 01:45
  • clean and simple. thanks! – Sobiaholic May 06 '13 at 22:43
  • According to the PHP site, both mysql_fetch_array and mysql_fetch_assoc will be deprecated, I don't know wether to laugh or cry. – andreszs Dec 02 '14 at 17:54
  • As mentioned at the beginning of the answer, the `mysql_*` functions are officially deprecated. Using MySQLi, the counterparts for these functions are [mysqli_fetch_row](http://php.net/manual/en/mysqli-result.fetch-row.php), [mysqli_fetch_assoc](http://php.net/manual/en/mysqli-result.fetch-assoc.php), and [mysqli_fetch_array](http://php.net/manual/en/mysqli-result.fetch-array.php), respectively. – Sean the Bean Sep 28 '17 at 17:33
8

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.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • This would be a more helpful answer if you mentioned why you would never use `mysql_fetch_array` (e.g., the performance impact, mixing associative and non-associative arrays, etc). – Sean the Bean Sep 28 '17 at 17:36
2

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"];
}
pyrometer
  • 890
  • 1
  • 8
  • 17