PHP mysql数据无法从上一个id中检索

I'm developing one API in php to display data on android app from my database using JSON.

In my app I want to display 20 records first, after display again 20 records once user scroll to top. I'm requesting the last id of the record from app to show next 20 records from last id.

Here is my code

<?php
$last_movie = 0;
$genre = $_REQUEST['genre'];
$last_movie = $_REQUEST['lastid'];

require_once("connect.php");
$myArray = array();

if($last_movie == 0)
{
    $result = $conn->query("SELECT * FROM my_movies WHERE genre = '$genre' ORDER BY year DESC LIMIT 20");
}
else
{
     $result = $conn->query("SELECT * FROM my_movies WHERE genre = '$genre' ORDER BY year LIMIT ".$last_movie.",20");

}

if ($result) {

    while($row = $result->fetch_array(MYSQL_ASSOC)) {
            $myArray[] = $row;
    }
    echo json_encode($myArray);
}

$result->close();
$conn->close();



?>

I'm getting values in some genres, but sometimes it show empty JSON. I tried with this url http://freemodedapk.com/bobmovies/by_genre.php?genre=Action

its working , whenever I try from last id http://freemodedapk.com/bobmovies/by_genre.php?genre=Action&lastid=4714

It returns empty JSON. I have values in database.


But some genres working fine http://freemodedapk.com/bobmovies/by_genre.php?genre=Drama http://freemodedapk.com/bobmovies/by_genre.php?genre=Drama&lastid=865

I have total 4858 records in the database with all genres.

Anybody can help me to fix empty JSON problems in some of genres ?

Your main issue is in the wrong LIMIT usage: when you utilize 2 parameters for LIMIT keyword, the first one is for OFFSET value (not for IDs), the second one is to limit your result.

In short words: you should use LIMIT 0,20 to get the first 20 results, LIMIT 20,20 to show next 20 results and so on.

Also, your code is insecure - you have SQL injection. Try to not post direct urls on your sites with the source code which includes injection because some bad guys may drop your database or do some other harmful things.

Sample code is listed below (minor changes may be required):

<?php

require_once('connect.php');

$response = [];

$items_per_page = 20;

$page = (int) (($_REQUEST['page'] - 1) * $items_per_page);

$genre = $conn->escape_string($genre); # replace escape_string with proper method if necessary

$result = $conn->query("SELECT * FROM my_movies WHERE genre = '$genre' ORDER BY year DESC LIMIT $page,$items_per_page");

if ($result)
{
    $response = $conn->fetch_all($result, MYSQLI_ASSOC); # replace fetch_all with proper method if necessary
}

echo json_encode($response);

$result->close();
$conn->close();

if you want to get last ID to ASC then use to

SELECT * FROM my_movies WHERE genre = '$genre' id<".$last_movie." ORDER BY year LIMIT 0,20

or if you want to get for pagination then your OFFSET value wrong,

you should be use LIMIT 0,20 to get the first 20 results, LIMIT 20,20 to next 20 , LIMIT 40,20

please check your SQL injection

look like Code

require_once('connect.php');
$result = [];
$limit = 20;
$pageNumber = (int) (($_REQUEST['pageNumber'] - 1) * $limit);
$genre = $conn->escape_string($genre);

$getDta = $conn->query("SELECT id,title,stream,trailer,directors,actors,quality,year,genre,length,translation,rating,description,poster FROM my_movies WHERE genre = '".$genre."' ORDER BY year DESC LIMIT $pageNumber,$limit");

if ($result)
    $result =$conn->fetch_all($result, MYSQLI_ASSOC);

echo json_encode($result);

$result->close();
$conn->close();