Balázs Vincze Balázs Vincze - 1 year ago 78
JSON Question

Fetch rows in MySQL and return JSON

I have the following SQL statement.

"SELECT * FROM table"

Than I do:
echo json_encode(mysqli_fetch_all($query));

Which gives me a json like this:


This is all fine, but it's missing the field names.

How could I get a JSON like this:

"id" : "32",
"name" : "John",
"phone" : "12346",
"email" : "",


Answer Source

it seems you get only numeric indizes. try fetching your data as associative array:

echo json_encode(mysqli_fetch_all($query, MYSQLI_ASSOC));

see also the manual

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download