Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Define db credentials - insert your own
- $DBHOST = "localhost";
- $DBUSER = "1";
- $DBPASS = "2";
- $DBNAME = "3";
- // connect to the db
- $con = new mysqli($DBHOST, $DBUSER, $DBPASS, $DBNAME);
- if ($con->connect_error) { trigger_error('Database connection failed: ' . $con->connect_error, E_USER_ERROR); }
- mysqli_set_charset($con,"utf8");
- // Limit - determine page number from $_GET - use "?page=" after url
- $page = 1;
- if(!empty($_GET['page'])) {
- $page = filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT);
- if(false === $page) {
- $page = 1;
- }
- }
- // Limit - set the number of items to display per page
- $items_per_page = 100;
- // Limit - build query
- $offset = ($page - 1) * $items_per_page;
- // sql query
- $query= "Select * FROM table ORDER BY date DESC LIMIT " . $offset . "," . $items_per_page; // insert your own table and order by
- $result = mysqli_query($con, $query) or die ("Could not execute query");
- // json result
- $jsonResult = [];
- while ($row = mysqli_fetch_array($result))
- {
- $jsonResult[] = $row;
- }
- header('Content-type: application/json; charset=utf8');
- echo json_encode($jsonResult);
- mysqli_free_result($result);
- mysqli_close($con);
- ?>
Add Comment
Please, Sign In to add comment