PHP - MySQL results to JSON

json, mysql, php

Solution

$result = mysqli_query($con, "SELECT * FROM Customers");   
while($row = mysqli_fetch_assoc($result))
    $test[] = $row; 
print json_encode($test);

Problem

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

Original source

Related problems