every time i run mysql_fetch_array an array is returned with duplicate values e.g.
Array
(
[0] => 1
[row_id] => 1
[1] =>
The mysql_fetch_array() returns indexed as well as associative array. However mysql_fetch_assoc() returns only associative-array.
When mysql_fetch_array used with MYSQLI_NUM parameter returns only an indexed array. Example: mysql_fetch_array($resultset,MYSQLI_NUM)
mysql_fetch_array()
returns an array containing both associative keys (the field names specified by queries) and integer position key (i.e. 0 is first field, 1, is second, etc.). This is for convenience in accessing the data by either method.
if you only want the named keys, you should use mysql_fetch_assoc()
or better yet, use the mysqli functions as everyone on here will lambast you for using the old mysql_* functions.
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)) { ... }
From the manual:
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), using MYSQL_NUM, you only get number indices (as mysql_fetch_row() works).
In other words, since MYSQL_BOTH is the default, you get both back. You can specify either MYSQL_ASSOC or MYSQL_NUM instead to get an associative array or numeric array back. You could also use mysql_fetch_assoc() instead which will only return an associative array.
Use mysql_fetch_assoc() for associtive array, or mysql_fetch_row for a numeric array
mysqli_fetch_array($result, MYSQL_ASSOC)
this has worked for me as suggested above for mysql_fetch_assoc
, in PHP5.5 it's deprecated now.