0

Hello guys here are my MySQL query which one getting data...

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

This code is working perfect, But when i am trying to get more data with AJAX i can't write the correct sql query syntax.

<?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);
  ...

I want to add WHERE video_id < ".$lastID." .. I tryed couple of combinations but everytime displaying syntax error.

Notes:

1- I am getting data from AJAX to $lastID;

2- $_SESSION ["userid"] is active, dont worry about this

3-SQL error is:

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

11
  • 1
    videos as v => videos v - you use AS for aliasing columns, not tables. Commented May 18, 2017 at 14:23
  • Take the AS out of your table alias for videos Commented May 18, 2017 at 14:23
  • first query is working godd just i want to add WHERE video_id < ".$lastID." to second query Commented May 18, 2017 at 14:24
  • I will try thank you for reply Commented May 18, 2017 at 14:26
  • I tryed couple of combinations without as but still same syntax error :) Commented May 18, 2017 at 14:29

1 Answer 1

1

Try to upercase your mysql keywords. its much better to read.

And you already have a WHERE, so you just need to combine it with an 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 

But change the ' ".$lastID." with a prepared statment.

Sign up to request clarification or add additional context in comments.

1 Comment

Thats it bro this is the correct answer thank you so much,$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"]));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.