Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql_fetch_array, mysql_fetch_assoc, mysql_fetch_object

The functions are all very similar:

mysql_fetch_array(), mysql_fetch_assoc(), mysql_fetch_object()

I have recently started using mysql_fetch_object as I am doing alot more OOP with PHP.

But what are peoples opinions on which one is best to use and why, and maybe which scenario they are best to be used in.

Thanks for your thoughts!

like image 498
Lizard Avatar asked Oct 08 '09 10:10

Lizard


People also ask

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

The mysqli_fetch_object() function returns objects from the database, whereas mysqli_fetch_array() function delivers an array of results. This will allow field names to be used to access the data.

What is 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 mysql_fetch_array () function?

mysql_fetch_array is a PHP function that will allow you to access data stored in the result returned from the TRUE mysql_query if u want to know what is returned when you used the mysql_query function to query a Mysql database. It is not something that you can directly manipulate. Syntax.

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.


1 Answers

mysql_fetch_array will get you an array that can have as keys :

  • both numbers and names of columns, if using MYSQL_BOTH
  • columns names, using MYSQL_ASSOC -- in this case, you'll get the same thing you get when using mysql_fetch_assoc
  • only numbers (depending on the order of columns in the query), if using MYSQL_NUM

Getting results indexed by columns names is probably the most useful solution -- easier to use, at least.

But getting results indexed by the positions of the fields in the select clause is interesting in one situtation : when you have several columns that have the same name or alias.
In this case, as you cannot have two entries with the same index in an array, you will be able to access only one of those columns using the column name as index.
For the other columns that have the same name, you'll have to use numeric indexes.

That situation is probably the only case for which I would use mysql_fetch_array -- and I rather prefer using aliases in my query, to avoid that situation -- it's more clear, in my opinion.


mysql_fetch_assoc will get you an array, with columns names as keys, and data as values.

Not much to say, actually.


And mysql_fetch_object will get you objetcs in return.


Choosing between mysql_fetch_assoc and mysql_fetch_object most probably depend on how you develop your application : if using objects everywhere, the second one is probably the most suited.

If using arrays as data-containers, you can just go with the first one.

like image 97
Pascal MARTIN Avatar answered Sep 25 '22 06:09

Pascal MARTIN