Difference Between MySQL_Fetch_Array and MySQL_Fetch_Row

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 )

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.

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.

Conflict between mysql_fetch_row and mysql_fetch_array

Instead of

mysql_fetch_row($select) != 0

use

mysql_num_rows($select) != 0

that will return an int for you to compare.

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.

mysql result fetching comparison using php

mysql_fetch_row()
It returns array with numeric index/key. Usually it is the faster compare with two other methods.

mysql_fetch_assoc()
It returns array with column name as key. It is slightly slower than mysql_fetch_row().

mysql_fetch_array()
It returns returns essentially two arrays. One with an associative based key index and one with a numeric index. mysql_fetch_array() without specifying which method you want (either MYSQL_NUM or MYSQL_ASSOC) always returns a double array. And it is considerably more inefficient as compared to mysql_fetch_row() or mysql_fetch_assoc(). You can set the result type as second parameter.

I think, actually those method has no significant difference.

mysql_fetch_array returns duplicate data

This is the intended functionality of mysql_fetch_array(). If you want to not have the "duplicates" and just have the associative-array, use mysql_fetch_assoc() instead.

Example:

while ($row = mysql_fetch_assoc($data)) { ... }

Using mysql_query and mysql_fetch_array

The difference is you're re-assigning the variables in the first example. But you could just say:

while(list($username, $password) = mysql_fetch_array($sql)) {
echo "$username:$password";
}

Or you could pull out a hash

while($row = mysql_fetch_assoc($sql)) {
echo "{$row['username']}:{$row['password']}";
}

The right way depends on the application or your preference, I personally avoid the numeric indexed arrays unless I specifically need them. Who wants to try to keep a mental tab of what data is in which index?

Understanding array notation (specifically mysql_fetch_array and print_r)

Associative arrays use strings for the index, numeric arrays use numbers.

mysql_fetch_array() creates an associative array containing numeric keys as well so that both may be used to access the array.

mysql_fetch_assoc() creates an array containing only the string indices.

mysql_fetch_row() creates an entirely numeric array, and is the fastest to execute of these functions.



Related Topics



Leave a reply



Submit