user3698774
user3698774

Reputation: 23

mysql get data into array show double data

I am trying to retrieve data from MySQL. But my result shows one result 2 time.

my code

$result = mysql_query($sql);
$rows = Array();
while($row = mysql_fetch_array($result)){
  array_push($rows, $row);
}
echo json_encode($rows);

my result

[{"0":"427","id":"427","1":"Alabama","title":"Alabama"}]

Upvotes: 1

Views: 62

Answers (2)

41eight
41eight

Reputation: 191

mysql_fetch_array returns both an associative and numeric array, if you would like to only fetch one or the other specify the result type as either MYSQL_NUM or MYSQL_ASSOC

Upvotes: 0

Jehad Keriaki
Jehad Keriaki

Reputation: 545

By default, mysql_fetch_array will fetch the data in both: array and associative

https://www.php.net/mysql_fetch_array

To overcome this issue, specify the type:

while($row = mysql_fetch_array($result, MYSQL_NUM)){

Or

while($row = mysql_fetch_array($result, MYSQL_ASSOC)){

Upvotes: 1

Related Questions