简体   繁体   English

无法从最后一个ID检索PHP mysql数据

[英]PHP mysql data not retrieving from last id

I'm developing one API in php to display data on android app from my database using JSON. 我正在用PHP开发一个API,以使用JSON从我的数据库中显示android应用程序上的数据。

In my app I want to display 20 records first, after display again 20 records once user scroll to top. 在我的应用中,我想先显示20条记录,然后在用户滚动到顶部后再次显示20条记录。 I'm requesting the last id of the record from app to show next 20 records from last id. 我正在从应用程序中请求记录的最后一个ID,以显示来自最后一个ID的下20条记录。

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. 我在某些类型中获取值,但有时它显示空的JSON。 I tried with this url http://freemodedapk.com/bobmovies/by_genre.php?genre=Action 我尝试使用此网址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 它的工作原理,每当我尝试使用最后一个ID http://freemodedapk.com/bobmovies/by_genre.php?genre=Action&lastid=4714时

It returns empty JSON. 它返回空的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 但是有些类型的作品效果不错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. 我所有类型的数据库中共有4858条记录。

Anybody can help me to fix empty JSON problems in some of genres ? 任何人都可以帮助我解决某些类型的空JSON问题吗?

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. 您的主要问题是使用了错误的LIMIT用法:当您为LIMIT关键字使用2个参数时,第一个参数用于补偿值(不适用于ID),第二个参数用于限制结果。

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. 简而言之:您应该使用LIMIT 0,20来获得前20个结果,使用LIMIT 20,20来显示下一个20个结果,依此类推。

Also, your code is insecure - you have SQL injection. 另外,您的代码也不安全-您有SQL注入。 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. 尽量不要使用包含注入的源代码在您的网站上发布直接URL,因为某些坏蛋可能会丢弃您的数据库或做一些其他有害的事情。

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 如果您想获取ASC的最后一个ID,请使用

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, 或者如果您想分页,则您的OFFSET值错误,

you should be use LIMIT 0,20 to get the first 20 results, LIMIT 20,20 to next 20 , LIMIT 40,20 您应该使用LIMIT 0,20来获取前20个结果,从LIMIT 20,20到下一个20,LIMIT 40,20

please check your SQL injection 请检查您的SQL注入

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM