7

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.

5
  • Show your query and your table structure. Commented Aug 14, 2012 at 17:24
  • 2
    Please don't use the mysql extension anymore! It's better to use PDO or MySQLi. Commented Aug 14, 2012 at 17:25
  • @ComFreek this is a private app that only I will use, for now its not a big problem using mysql but when I have time i'll change to MySqlLi Commented Aug 14, 2012 at 17:28
  • @mk_89: you didn't post your query and table structure. Hard to give accurate answers without that. Commented Aug 14, 2012 at 17:42
  • with actual PDO, use $row = $streq->fetch(PDO::FETCH_ASSOC) Commented Jul 27, 2023 at 13:22

7 Answers 7

15

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)) { ... }
Sign up to request clarification or add additional context in comments.

3 Comments

mysql_fetch_array($data, MYSQL_ASSOC); will do exactly the same as mysql_fetch_assoc.
@Jocelyn I am aware of that - however - there is no way using MYSQL_ASSOC would return an array with the output the OP displayed, as per the manual.
@mk_89 I just tested with mysql_fetch_array($data, MYSQL_ASSOC) and cannot replicate (i.e. - it's working how it should) so I cannot answer as to why it would fail in your case. Try using mysql_fetch_assoc() and see if that resolves the issue.
5

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.

Comments

1

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

1 Comment

mysql_fetch_array($data, MYSQL_ASSOC); will do exactly the same as mysql_fetch_assoc.
1

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.

2 Comments

mysql_fetch_array($data, MYSQL_ASSOC); will do exactly the same as mysql_fetch_assoc.
@Jocelyn Yes it will. I don't understand the downvote though, as most developer who were familiar with using the old mysql_* functions for any period of time would typically use mysql_fetch_assoc and not have to worry about setting the proper flag on the function call.
1

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.

Comments

0

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.

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.