Guest User

Untitled

a guest
Apr 21st, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. <?php
  2. // Define db credentials - insert your own
  3. $DBHOST = "localhost";
  4. $DBUSER = "1";
  5. $DBPASS = "2";
  6. $DBNAME = "3";
  7. // connect to the db
  8. $con = new mysqli($DBHOST, $DBUSER, $DBPASS, $DBNAME);
  9. if ($con->connect_error) { trigger_error('Database connection failed: ' . $con->connect_error, E_USER_ERROR); }
  10. mysqli_set_charset($con,"utf8");
  11. // Limit - determine page number from $_GET - use "?page=" after url
  12. $page = 1;
  13. if(!empty($_GET['page'])) {
  14. $page = filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT);
  15. if(false === $page) {
  16. $page = 1;
  17. }
  18. }
  19. // Limit - set the number of items to display per page
  20. $items_per_page = 100;
  21. // Limit - build query
  22. $offset = ($page - 1) * $items_per_page;
  23. // sql query
  24. $query= "Select * FROM table ORDER BY date DESC LIMIT " . $offset . "," . $items_per_page; // insert your own table and order by
  25. $result = mysqli_query($con, $query) or die ("Could not execute query");
  26. // json result
  27. $jsonResult = [];
  28. while ($row = mysqli_fetch_array($result))
  29. {
  30. $jsonResult[] = $row;
  31. }
  32. header('Content-type: application/json; charset=utf8');
  33. echo json_encode($jsonResult);
  34. mysqli_free_result($result);
  35. mysqli_close($con);
  36. ?>
Add Comment
Please, Sign In to add comment