2017-05-18 60 views
0

你好這裏人是我的MySQL查詢哪一個獲取數據...難SQL查詢語法

$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." ..我tryed的情侶組合,但每次顯示語法錯誤。

注:

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

+1

'video as v' =>'videos v' - 您使用'AS'來別名列而不是表格。 – Qirel

+0

把你的表別名視頻 –

+0

第一個查詢正在工作godd只是我想添加WHERE video_id <「。$ lastID。」'第二個查詢 –

回答

1

嘗試大寫在前你的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。」並附有準備好的資料。

+0

多數民衆贊成這是正確的答案非常感謝你,'$ videofetch = $ conn-> prepare(「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「); $ videofetch-> execute(array($ _ SESSION [「userid」]));' –