Advertisement
laurens-wuyts

db_view.php

May 12th, 2015
4,786
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.07 KB | None | 0 0
  1. <?php
  2. // array for JSON response
  3. $response = array();
  4.  
  5. // include db connect class
  6. require_once __DIR__ . '/db_connect.php';
  7.  
  8. // connecting to db
  9. $db = new DB_CONNECT();
  10.  
  11. // get all products from products table
  12. $result = mysql_query("SELECT *FROM Parts") or die(mysql_error());
  13.  
  14. // check for empty result
  15. if (mysql_num_rows($result) > 0) {
  16.     // looping through all results
  17.     // products node
  18.     $response["Parts"] = array();
  19.  
  20.     while ($row = mysql_fetch_array($result)) {
  21.         // temp user array
  22.         $Part = array();
  23.         $Part["ID"] = $row["ID"];
  24.         $Part["Name"] = $row["Name"];
  25.         $Part["part_nr"] = $row["part_nr"];
  26.  
  27.         // push single product into final response array
  28.         array_push($response["Parts"], $Part);
  29.     }
  30.     // success
  31.     $response["success"] = 1;
  32.  
  33.     // echoing JSON response
  34.     echo json_encode($response);
  35. } else {
  36.     // no products found
  37.     $response["success"] = 0;
  38.     $response["message"] = "No parts inserted";
  39.  
  40.     // echo no users JSON
  41.     echo json_encode($response);
  42. }
  43. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement