Return JSON from MySQL with Column Name Return JSON from MySQL with Column Name arrays arrays

Return JSON from MySQL with Column Name


Use mysqli_fetch_assoc()

Here you go

$jsonData = array();if(mysqli_num_rows($result) > 0){while ($array = mysqli_fetch_assoc($result)) {    $jsonData[] = $array;}$json = json_encode($jsonData);echo stripslashes($json);}


You should try while($row = mysqli_fetch_assoc($result)).

It should return the result with the respective fieldnames.

You can find the manual page here.


You should use mysqli_fetch_assoc here instead so that it returns the key as the column names. mysqli_fetch_row returns numeric array keys instead.

Try this:

if (mysqli_num_rows($result) > 0) {   $jsonData[] =  mysqli_fetch_assoc($result);}$json = json_encode($jsonData, JSON_PRETTY_PRINT);