-1

I'm using the following script to retrieve the nth record from a table:

if($last_published_fb < $numposts){
    $last_published_fb--;
    // retrieve first post with id > last_published_fb
    $connect = dbconn(PROJHOST, POSTSDB, POSTSUSR, POSTSPWD);
    $sql = "SELECT * FROM tblposts ORDER BY id LIMIT :max,1";
    $query = $connect->prepare($sql);
    $query->bindParam(':max', $last_published_fb);
    if($query->execute()) {
        $rows = $query->fetchObject();
        $newid = ($rows ? $rows->id : null);
        echo 'to be published: ' . $newid;
    }
}

Basically, if $last_published_fb is 0, I need to retrieve the 1st row from the table, if it's 4, I need the 5th row, and so on. However, the above code refuses to work and throws a syntax error in the SQL statement. Am I missing something? Here's the error message:

PHP Fatal error:  Uncaught exception 'PDOException' with message '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 ''0',1' at line 1' in /xxxx/xxx/xxx/posts.php:39
TheLearner
  • 2,813
  • 5
  • 46
  • 94

1 Answers1

0

Try to cast $last_published_fb to an integer

 $query->bindParam(':max', (int) $last_published_fb, PDO::PARAM_INT);
Tim S.
  • 391
  • 1
  • 5