"php mysql data to json file" Code Answer

2

you can use json_encode function.

  1. fetch data from db and assign it to an array
  2. then use json_encode($result_array). this will produce the json result. click here
  3. use file_put_contents function to save the json result to your .json file

following is an example code,

$result = mysql_query(your sql here);    
$data = array();
while ($row = mysql_fetch_assoc($result)) {
    // generate the output in desired format
    $data = array(
        'cols' => ....
        'rows' => ....
        'p' => ...
    );
}

$json_data = json_encode($data);
file_put_contents('your_json_file.json', $json_data);
By David Aldridge on July 27 2022

Answers related to “php mysql data to json file”

Only authorized users can answer the Search term. Please sign in first, or register a free account.