Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. <?php
  2. // Create connection to database
  3. $con=mysqli_connect("localhost","root","root","MyHolidaysDatabase");
  4.  
  5. // Check connection
  6. if (mysqli_connect_errno())
  7. {
  8. echo "Failed to connect to MySQL: " . mysqli_connect_error();
  9. }
  10.  
  11. // month value sent from the client with a POST request
  12. $month = $con->real_escape_string($_POST['month']);
  13. $stmt = $con->prepare("SELECT * FROM Holidays WHERE month = ?");
  14. $stmt->bind_param("s", $month);
  15. $stmt->execute();
  16. $result = $stmt->get_result();
  17. $json_array = array();
  18.  
  19. // Prepares all the results to be encoded in a JSON
  20. while ($row = $result->fetch_assoc())
  21. {
  22. $hokiday_id = $row['hid'];
  23. $holiday_month= $row['month'];
  24. $holiday_description =$row['description'];
  25. $holiday_day = $row['day'];
  26. $holiday_year = $row['year'];
  27. $holiday = array("hid" => $hokiday_id, 'month' => $holiday_month, 'day' => $holiday_day, 'year' => $holiday_year, 'description'=> $holiday_description);
  28. array_push($json_array, $holiday);
  29. }
  30.  
  31. // encodes array with results from database
  32. echo json_encode($json_array);
  33. mysqli_close($con);
  34. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement