Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql_fetch_row() vs mysql_fetch_assoc() vs mysql_fetch_array() [duplicate]

Tags:

mysql

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.

like image 541
user1479431 Avatar asked Jul 14 '12 01:07

user1479431


People also ask

What is the difference between mysql_fetch_array () and mysql_fetch_assoc ()?

Difference: mysql_fetch_assoc() will always assing a non-secuencial key (like "color" and not a number). mysql_fetch_array() will assing a number key if there are not "word" key (0 and not "color" if there are not "color") but, you can choose to assing of key with a parameter...

What is difference between mysql_fetch_array and Mysql_fetch_row?

mysqli_fetch_array() is an extended version of the mysqli_fetch_row() function. In addition to storing the data in the numeric indices of the result array, the mysqli_fetch_array() function can also store the data in associative indices, using the field names of the result set as keys.

What is the difference between mysqli_fetch_array () and Mysqli_fetch_assoc () function?

The major difference between mysqli_fetch_assoc and mysqli_fetch_array is the output format of result data. mysqli_fetch_assoc returns data in an associative array and mysqli_fetch_array returns data in a numeric array and/or in an associative array.

What is the function of mysql_fetch_assoc?

Definition and Usage The fetch_assoc() / mysqli_fetch_assoc() function fetches a result row as an associative array. Note: Fieldnames returned from this function are case-sensitive.


3 Answers

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.

like image 185
Filip Roséen - refp Avatar answered Oct 23 '22 04:10

Filip Roséen - refp


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.

like image 34
Niet the Dark Absol Avatar answered Oct 23 '22 03:10

Niet the Dark Absol


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"];
}
like image 21
pyrometer Avatar answered Oct 23 '22 05:10

pyrometer