大家好,这是我的 MySQL 查询,其中一个正在获取数据...
$videofetch = $conn->prepare("select * from user_followers as uf join videos as v on uf.followed_id = v.publisher_id where uf.follower_id = ? order by video_id desc limit 5");
$videofetch->execute(array(@$_SESSION ["userid"]));
$vid = $videofetch->fetchALL(PDO::FETCH_ASSOC);
这段代码工作完美,但是当我尝试使用 AJAX 获取更多数据时,我无法编写正确的 sql 查询语法。
<?php
session_start();
if(isset($_POST["id"]) && !empty($_POST["id"])) {
include('connectdb.php');
$lastID = $_POST['id'];
$videofetch = $conn->prepare("select * from user_followers as uf join videos as v on uf.followed_id = v.publisher_id where uf.follower_id = ? order by video_id desc limit 5");
$videofetch->execute(array($_SESSION["userid"]));
$vid = $videofetch->fetchALL(PDO::FETCH_ASSOC);
...
我要添加WHERE video_id < ".$lastID."
..我尝试了几种组合,但每次都显示语法错误。
注释:
1- 我正在从 AJAX 获取数据到 $lastID
;
2- $_SESSION ["userid"]
处于事件状态,不用担心这个
3-SQL错误是:
Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as v on uf.followed_id = v.publisher_id where uf.follower_id = '1' order by vid' at line 1 in C:\wamp64\www\hola.com\functions\getdatafoll.php on line 9
最佳答案
尝试将 mysql 关键字大写。阅读起来要好得多。
而且您已经有了一个 WHERE,因此只需将它与 AND 组合起来即可。
SELECT *
FROM user_followers AS uf
JOIN videos AS v ON uf.followed_id = v.publisher_id
WHERE uf.follower_id = ?
AND video_id < ".$lastID."
ORDER BY video_id DESC
LIMIT 5
但是更改'“.$lastID”。并附上一份准备好的声明。
关于php - 困难的 SQL 查询语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44050443/