Veltar
Veltar

Reputation: 741

PHP adds mysql-results double to array.

$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?

Upvotes: 1

Views: 269

Answers (3)

xkeshav
xkeshav

Reputation: 54022

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

Upvotes: 0

Ken Gregory
Ken Gregory

Reputation: 7390

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

Upvotes: 0

Haim Evgi
Haim Evgi

Reputation: 125496

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

Upvotes: 2

Related Questions