MySQL_Fetch_Array, MySQL_Fetch_Assoc, MySQL_Fetch_Object

mysql_fetch_array, mysql_fetch_assoc, mysql_fetch_object

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.

mysql_fetch_row() vs mysql_fetch_assoc() vs mysql_fetch_array()


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.

Is mysql_fetch_array a the combination of mysql_fetch_assoc and mysql_fetch_row?

mysql_fetch_assoc returns an associative array, mysql_fetch_row returns a numeric array, and with mysql_fetch_array you can chose what would be the output. This function accepts an optional parameter which can take values:

  • MYSQL_ASSOC - returns associative array
  • MYSQL_NUM - returns numeric array
  • MYSQL_BOTH - returns combined numeric and associative array

The last value is default.

mysql_fetch_object is slightly different as it returns an object wich has fields corresponding to columns in result fetched from database.

As a sidenote I would like to add that mysql_* functions are deprecated and you should switch to mysqli or PDO.

Difference between mysql_fetch_array and mysql_fetch_row?

The documentation is pretty clear on this, have you looked at it ?

mysql_fetch_array ( resource $result [, int $result_type = MYSQL_BOTH ] )

Returns an array of strings that corresponds to the fetched row, or
FALSE if there are no more rows. The type of returned array depends on
how result_type is defined.
By using MYSQL_BOTH (default), you'll get
an array with both associative and number indices. Using MYSQL_ASSOC,
you only get associative indices (as mysql_fetch_assoc() works), [by] using
MYSQL_NUM, you only get number indices (as mysql_fetch_row() works)
.

mysql_fetch_row ( resource $result )

Returns an numerical array of strings that corresponds to the fetched
row, or FALSE if there are no more rows.

mysql_fetch_row() fetches one row of data from the result associated
with the specified result identifier. The row is returned as an array.
Each result column is stored in an array offset, starting at offset 0.

In summary

mysql_fetch_array( $result, MYSQL_ASSOC ) = mysql_fetch_assoc( $result )
mysql_fetch_array( $result, MYSQL_NUM ) = mysql_fetch_row( $result )

And

mysql_fetch_array ( $result ) = mysql_fetch_assoc( $result ) + mysql_fetch_row( $result )

What's better solution: mysql fetch array than object or mysql join

Whatever is handled at the database is generally faster than whatever is handled at the scripting side. So i would recommend that always go with the joins. your second part of code where you have used the SQL JOIN is better.

e.g. you can easily see that in the first code there is one extra call being made to the database which is not the case in the second code. So for every entry that is fetched in the first loop you will have to again look into the database. e.g. your first loop returns 100 records then in the second loop it will look in the database again for 100 times. so it makes it slow.

mysql_fetch_array() works but mysql_fetch_object() doesn't

With fetch_object the syntax is :

$row->date1 

with fetch_assoc the syntax is:

$row["date1"]

Also please.. for everything that is holy.. Turn E_NOTICE error messaging and display_errors on

mysql_fetch_object() or mysql_fetch_array() which is better and why?

Both are good. Choose what you like more - objects or arrays.

It's even better to use PDO or mysqli instead of mysql extension.



Related Topics



Leave a reply



Submit