Advertisement
AMONRA75

PHP - MYSQL TO JSON

Jun 28th, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.90 KB | None | 0 0
  1. <?php
  2.  
  3. require 'functions.php';
  4. //setting header to json
  5. header('Content-Type: application/json');
  6.  
  7. //database
  8. define('DB_HOST', '127.0.0.1');
  9. define('DB_USERNAME', 'root');
  10. define('DB_PASSWORD', '');
  11. define('DB_NAME', 'binax');
  12.  
  13. //get connection
  14. $mysqli = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);
  15.  
  16. if(!$mysqli){
  17.   die("Connection failed: " . $mysqli->error);
  18. }
  19.  
  20. //query to get data from the table
  21. $query = "SELECT DATE_FORMAT(timeof, '%d-%m-%Y') AS giorno, FORMAT(SUM(totsell-cost),2) AS somma FROM POSITION WHERE DATE_FORMAT(timeof, '%d-%m') GROUP BY giorno ORDER BY timeof ASC";
  22.  
  23. //execute query
  24. $result = $mysqli->query($query);
  25.  
  26. //loop through the returned data
  27. $data = array();
  28. foreach ($result as $row) {
  29.   $data[] = $row;
  30. }
  31.  
  32. //free memory associated with result
  33. $result->close();
  34.  
  35. //close connection
  36. $mysqli->close();
  37.  
  38. //now print the data
  39. print json_encode($data);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement