php converts data from database into json format and outputs it

  • 2021-11-01 02:46:57
  • OfStack

As shown below:


header('content-type:application/json;charset=utf8');

$results = array();

 while ($row = mysql_fetch_assoc($result_query)) {
 $results[] = $row;
 }
 
 if($results){
 echo json_encode($results);
 
 }else{
 echo mysql_error();
 }

Save the queried array into a new array and return to json format (the statement in the query section has been omitted).


Related articles: