duandanbeng1829 2017-01-24 09:54
浏览 29

使用PDO从表中检索第n条记录时出错[复制]

This question already has an answer here:

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
</div>
  • 写回答

1条回答 默认 最新

  • douben1891 2017-01-24 10:10
    关注

    Try to cast $last_published_fb to an integer

     $query->bindParam(':max', (int) $last_published_fb, PDO::PARAM_INT);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式