Advertisement
Guest User

data.php

a guest
Nov 30th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.22 KB | None | 0 0
  1. <?php
  2.     header('Content-Type: application/json');
  3.    
  4.     //SQL-information
  5.     $dbservername = "d0020e.noexit.tv";
  6.     $dbusername = "d0020e_data";
  7.     $dbpassword = "hidden";
  8.     $dbname = "d0020e_data";
  9.    
  10.     //Create new mysql connection
  11.     $conn = new mysqli($dbservername, $dbusername, $dbpassword, $dbname);
  12.    
  13.     if ($conn->connect_error) {
  14.         die("Connection failed: " . $conn->connect_error);
  15.     }
  16.     else
  17.     {
  18.         if(isset($_GET['n'])){
  19.        
  20.             if($_GET['n'] == all) {
  21.                 //Query all points
  22.                 $sql = "SELECT * FROM test_dp ORDER BY tstamp DESC";
  23.             }
  24.             else {
  25.                 //Query n number of points
  26.                 $limit = intval($_GET['n']);
  27.                 $sql = "SELECT * FROM test_dp ORDER BY tstamp DESC LIMIT $limit";
  28.             }
  29.         }
  30.         else { 
  31.             //n is not set, query a default number of 360 points
  32.             $limit = 360;
  33.             $sql = "SELECT * FROM test_dp ORDER BY tstamp DESC LIMIT $limit";
  34.         }
  35.         $data_points = array();
  36.         $result = $conn->query($sql);
  37.  
  38.         //Add all points to array and echo in JSON format
  39.         while($row = $result->fetch_assoc())
  40.         {        
  41.             $point = array("x" => $row['tstamp'] , "y" => $row['y']);
  42.             array_push($data_points, $point);          
  43.         }
  44.         echo json_encode($data_points, JSON_NUMERIC_CHECK);
  45.     }
  46.     $conn->close();
  47. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement