Wednesday, 21 November 2018

How to create JSON object in PHP

Hi all,

Use this code

<?php

$con= new mysqli('localhost','user','huppassword','databasename')or die("Could not connect to mysql".mysqli_error($con));

$qr="select * from questions";

$result = mysqli_query($con, $qr);

$data = array();

while ( $row = $result->fetch_assoc() ){
   // $data[] = json_encode($row);
   $data[]=$row;
}
    echo json_encode( $data );//this will be sent as response to the requesting device

  
?>

4 comments: