1

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"}]
1
  • 2
    RTFM: php.net/mysql_fetch_array the default fetch mode is FETCH_BOTH, which fetches string AND numeric keys Commented Jun 3, 2014 at 21:36

2 Answers 2

1

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

Comments

0

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

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.