douyu2817 2014-05-28 07:11
浏览 16
已采纳

我的PDO mysql查询有什么问题[重复]

This question already has an answer here:

Here is my code

$sql3= "select * 
        from comments 
        where status=:status  
        limit=:limit 
        offset=:offset 
        order by time desc";
$query3= $pdo->prepare($sql3);
$query3->bindValue(":status",'n');
$query3->bindValue(":limit",$per_page);
$query3->bindValue(":offest",$offset);
$query3->execute();
$comments=$query3->fetchall();

Here comments is my table name status and time is two column in my table . Whenever I run this code , It shows a warning

Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in E:\XAMPP\htdocs\parlament\user\logged_in_area.php

What does this mean ?

</div>
  • 写回答

6条回答 默认 最新

  • douci6541 2014-05-28 07:23
    关注

    The answer depends of what are limit and offset.

    If they're columns names...

    • You can't use these reserved keywords for column names without backticks
    • You need to add a AND / OR operator between your lines
    $sql3= "select * 
            from comments 
            where status=:status  
            and `limit`=:limit 
            and `offset`=:offset 
            order by time desc";
    $query3= $pdo->prepare($sql3);
    $query3->bindValue(":status", 'n');
    $query3->bindValue(":limit", $per_page);
    $query3->bindValue(":offest", $offset);
    $query3->execute();
    $comments=$query3->fetchall();
    

    If they're keywords...

    • The syntax is LIMIT <n>, not LIMIT = <n> (same for OFFSET)
    • It better to specify their type with PDO::PARAM_INT (same for OFFSET)
    • ORDER BY must be added before LIMIT and OFFSET
    $sql3= "select * 
            from comments 
            where status=:status 
            order by time desc 
            limit :limit 
            offset :offset";
    $query3= $pdo->prepare($sql3);
    $query3->bindValue(":status", 'n');
    $query3->bindValue(":limit", (int)$per_page, PDO::PARAM_INT);
    $query3->bindValue(":offset", (int)$offset, PDO::PARAM_INT);
    $query3->execute();
    $comments=$query3->fetchall();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要