Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - MySQL results to JSON

Tags:

json

php

mysql

I'm trying to understand how to convert MySQL results to a JSON format so that I can then use this JSON later on with Javascript to build a HTML table. However my code just produces lot's of null values and I don't yet understand why.

$result = mysqli_query($con, "SELECT * FROM Customers");

$test = json_encode($result);

print $test;

Output:

{"current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":null}

I have, for example, fields such as "CustomerID" and "Name", and even they don't show up in the JSON result.

What am I doing wrong? Thanks

like image 547
user9993 Avatar asked Apr 21 '14 16:04

user9993


1 Answers

$result = mysqli_query($con, "SELECT * FROM Customers");   
while($row = mysqli_fetch_assoc($result))
    $test[] = $row; 
print json_encode($test);
like image 99
Alexey Palamar Avatar answered Oct 22 '22 21:10

Alexey Palamar