1

Using PHP PDO I run this query

SELECT Name, Latitude, Longitude, Begin, End FROM VenueData

And fetch the data

$data = $sth->fetchAll();

Then output

header("Content-type: application/json");
print(json_encode(array('venues'=>$data)));

However the output is strange,

{"venues":[{"Name":"Flintstone1","0":"Flintstone1","Latitude":"57","1":"57","Longitude":"-124","2":"-124","Begin":"8","3":"8","End":"14","4":"14"}]}

It seems for every column in the select query there are 2 keys, one by name and one by index 0-n) in the json. I have not encountered this before, why is this happening?

2 Answers 2

7

This is probably because of the fetch you are using.

Check: PHP Manual PDOStatement::fetchAll

By default this will use PDO::FETCH_BOTH which includes numerative and associate array of results.

(The fetch style options can be found here: PHP Manual PDOStatement::fetch)

Depending on what you want, you can either use:

$data = $sth->fetchAll(PDO::FETCH_ASSOC);

Or:

$data = $sth->fetchAll(PDO::FETCH_NUM);
Sign up to request clarification or add additional context in comments.

Comments

0

Change this:

$data = $sth->fetchAll();

By this

$data = $sth->fetchAll(PDO::FETCH_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.