Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql_fetch_array returns duplicate data

Tags:

php

mysql

every time i run mysql_fetch_array an array is returned with duplicate values e.g.

Array
(
    [0] => 1
    [row_id] => 1
    [1] => some text
    [first_field] => some text
    [2] => some text
    [second_field] => some text 
}

but I only want single results in the array, I have tried using

mysql_fetch_array($data, MYSQL_ASSOC);

But this makes no difference.

like image 599
mk_89 Avatar asked Aug 14 '12 17:08

mk_89


4 Answers

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)) { ... }
like image 57
newfurniturey Avatar answered Sep 25 '22 02:09

newfurniturey


mysql_fetch_array returns resultset returned as as response to query execution as both associative and numeric arrays. For returning resultset as associative array you need to use mysql_fetch_assoc function. For returning resultset as numeric array you need to use mysql_fetch_row function.

like image 34
Rubin Porwal Avatar answered Sep 23 '22 02:09

Rubin Porwal


Use mysql_fetch_assoc() for associtive array, or mysql_fetch_row for a numeric array

like image 33
Clarence Avatar answered Sep 24 '22 02:09

Clarence


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.

like image 40
Mike Brant Avatar answered Sep 27 '22 02:09

Mike Brant