Shockrate

mysql to json

Nov 3rd, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. header('Content-Type: application/json');
  2.  
  3. //get the data from table ‘books’
  4. $query = "SELECT * FROM NOMETABELLA";
  5.  
  6. //execute the query
  7. $result = mysqli_query($link, $query);
  8.  
  9. if(!$result){
  10. echo "Couldn't execute the query";
  11. die();
  12. }
  13. else{
  14. //creates an empty array to hold data
  15. $data = array();
  16. while($row = mysqli_fetch_assoc($result)){
  17. $data[]=$row;
  18. }
  19. // echo json_encode($data, JSON_PRETTY_PRINT);
  20. //it will create file results.json with writing mode.
  21. //you can read more about file handling in PHP here.
  22. $fp = fopen('NOMEFILE.json', 'w');
  23. //json_enconde($array, $options(optional) is the method to convert array into JSON
  24. fwrite($fp, json_encode($data, JSON_PRETTY_PRINT));
  25. //close the file
  26. fclose($fp);
  27. }
Advertisement
Add Comment
Please, Sign In to add comment