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 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行