Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我的PDO mysql查询有什么问题_Php_Mysql_Sql_Pdo - Fatal编程技术网

Php 我的PDO mysql查询有什么问题

Php 我的PDO mysql查询有什么问题,php,mysql,sql,pdo,Php,Mysql,Sql,Pdo,这是我的密码 $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(":

这是我的密码

$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();
这里的注释是我的表名状态,时间是我表中的两列。每当我运行此代码时,它都会显示一条警告

警告:PDOStatement::execute():SQLSTATE[HY093]:无效参数编号:未在E:\XAMPP\htdocs\parlament\user\logged_in_area.php中定义参数


这意味着什么?

语句中缺少
关键字。另外,
LIMIT
是一个变量,如果您不想这样做,您需要将其反勾选或重命名为其他名称。

对此不确定,但我认为PDO::bindValue/bindParam可用于变量引用。不能将静态值设置为参数。
$sql3= "select * 
        from comments 
        where status = ?  
        limit= ?
        offset= ? 
        order by time desc";
$query3= $pdo->prepare($sql3);
$query3->bindValue(1,'n');
$query3->bindValue(2,$per_page);
$query3->bindValue(3,$offset);
$query3->execute();
$comments=$query3->fetchall();
试着替换

$query3->bindValue(":status",'n'); 

您还忘记了条件之间的“AND”关键字

试试这个->

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

答案取决于什么是
限制
偏移

如果它们是列名。。。
  • 如果没有反勾号,则无法对列名使用这些保留关键字
  • 您需要在两行之间添加
    /
    运算符
如果它们是关键字。。。
  • 语法是
    LIMIT
    ,而不是
    LIMIT=
    (与
    偏移量相同)
  • 最好使用
    PDO::PARAM_INT
    指定它们的类型(与
    偏移量相同)
  • 必须在
    限制
    偏移

添加我的答案,因为还没有人提到这一特定部分

MySQL对
LIMIT
参数的数据类型非常挑剔。您非常需要使用
bindParam(':limit',$per_page,PDO::PARAM_INT)
。我假设
偏移量
也是如此

总之

// because E_WARNING level errors are insufficient
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$stmt = $pdo->prepare('SELECT * FROM `comments` WHERE `status` = :status ORDER BY `time` DESC LIMIT :limit OFFSET :offset');
$stmt->bindValue(':status', 'n');
$stmt->bindParam(':limit', $per_page, PDO::PARAM_INT);
$stmt->bindParam(':offset', $offset, PDO::PARAM_INT); // spelt "offset"
$stmt->execute();
$comments = $stmt->fetchAll(PDO::FETCH_ASSOC);


首先,您的条件之间缺少
关键字这是什么查询?1.限制并不是最终的结果。2.什么是偏移量?不要使用limit=:limit将其更改为limit:limit,与offset相同..你们需要阅读手册~还有一件似乎没有提到的事情:你们的查询使用了占位符
:offset
,但你们将一个值绑定到
:offest
。我同意--解释一下。他是对的。OP格式化这个的方式,limit只是他表中的另一列。我想说OP使用的是标准用法中的
limit
。我假设在这种情况下
limit
是一列,因为他还有一个
offset
列。如果限制在最后,我就不会说我做了什么。检查手册<代码>限制。。。偏移量
已完全消除valid@Phil我会被诅咒的。你说得对。你只是把limit当作表中的另一列。虽然您的意思可能是限制结果的
LIMIT
关键字。虽然正确,但这不是完整答案,因为查询仍然会失败。实际上,这是不正确的<代码>绑定值
特别适用于非-references@Phil啊..对…没有注意到
bindValue
部分如果它们是关键字,就不应该限制和抵消订单吗?@BojanKovacevic你说得对,我刚刚意识到了这一点。谢谢@zessx np,我知道c/p问题:)@Phil当我在查询中按后限下单时,我总是得到sql错误。也许它在一些新版本中发生了变化:D@BojanKovacevic刚刚测试过,你是对的<代码>订购人
必须在
限额
$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();
$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();
// because E_WARNING level errors are insufficient
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$stmt = $pdo->prepare('SELECT * FROM `comments` WHERE `status` = :status ORDER BY `time` DESC LIMIT :limit OFFSET :offset');
$stmt->bindValue(':status', 'n');
$stmt->bindParam(':limit', $per_page, PDO::PARAM_INT);
$stmt->bindParam(':offset', $offset, PDO::PARAM_INT); // spelt "offset"
$stmt->execute();
$comments = $stmt->fetchAll(PDO::FETCH_ASSOC);