1
$query = "select * from tblRecords where record_category = '" . $cat . "' order by record_artist";
$result = $link->query($query);

This is the query that I use, to fetch the results, this works ok.

while($record = $result->fetch_array()) {
        array_push($arr_result, $record);
}

And this is the code I add each value to the array. But after

echo json_encode($arr_result);

This is the result set I receive:

https://i.sstatic.net/Gmggl.png

What am I doing wrong?

1
  • try var_dump($record) in your while loop Commented May 19, 2011 at 12:47

3 Answers 3

2

you need to pass = > MYSQL_ASSOC value to fetch_array function

look on : http://php.net/manual/en/function.mysql-fetch-array.php

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).

Sign up to request clarification or add additional context in comments.

1 Comment

In other words, the fetch_array() version creates both a regular array AND hash version of the results.
0

There is no function like fetch_array() in PHP, I think you're using a predefined MySQL class which internally calls mysql_fetch_array(), so check the class, actually you are getting correct result but by using MYSQL_BOTH (default), you'll get an array with both associative and number indices.

Use

$row = mysql_fetch_array($result, MYSQL_ASSOC));

Or

$row = mysql_fetch_array($result, MYSQL_NUM));

You must have forgotten to write a argument with fetch_array().

reference

2 Comments

MySQLi resultsets have a method called fetch_array.
@Emil Vikström OP does not describe about that, so i was expecting as general php function with mysql. anyway thanks for knowledge
0

fetch_array is returning both a numeric and an associative array - instead, use fetch_array_assoc()

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.