Advertisement
Guest User

Untitled

a guest
Apr 4th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. <?php
  2. header('Access-Control-Allow-Origin: *');
  3. // Variables
  4.  
  5. // Connect to Database
  6. $username = "database_user";
  7. $password = "user_password";
  8. $servername = "localhost"; // if the php file and the sql server reside on the same server, it is usually localhost
  9. $dbname = "database_name";
  10.  
  11. $id = $_GET['id'];
  12. $table = $_GET['table'];
  13.  
  14. // Create connection
  15. $conn = new mysqli($servername, $username, $password, $dbname);
  16.  
  17. // Check connection
  18. if ($conn->connect_error) {
  19. die("Connection failed: " . $conn->connect_error);
  20. }
  21.  
  22.  
  23. //create an array
  24. $emparray = array();
  25. while(1) {
  26. // Query
  27. $sql = "SELECT * FROM `" . $table . "` WHERE id = '" . $id . "'";
  28.  
  29. // Result
  30. $result = mysqli_query($conn, $sql) or die("Error in Selecting " . mysqli_error($conn));
  31.  
  32. $row = mysqli_fetch_assoc($result);
  33. $emparray[] = $row;
  34. if ($row["parent_id"] == NULL) {
  35. break;
  36. } else {
  37. $id = $row["parent_id"];
  38. }
  39. }
  40.  
  41. echo json_encode($emparray);
  42.  
  43. $conn->close();
  44. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement