我的PDO mysql查询有什么问题

时间:2014-05-28 07:11:51

标签: 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(":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中定义

这是什么意思?

6 个答案:

答案 0 :(得分:2)

答案取决于limitoffset

如果他们是列名......

  • 如果没有反引号
  • ,则不能将这些保留关键字用于列名
  • 您需要在行之间添加AND / OR运算符
$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();

如果他们是关键词......

  • 语法为LIMIT <n>,而非LIMIT = <n>OFFSET相同)
  • 最好使用PDO::PARAM_INT指定类型(OFFSET相同)
  • 必须在ORDER BYLIMIT 之前添加
  • 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();

答案 1 :(得分:1)

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

MySQL对LIMIT参数的数据类型非常挑剔。您几乎需要使用bindParam(':limit', $per_page, PDO::PARAM_INT)。我对OFFSET假设相同。

所以,总结

// 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);

答案 2 :(得分:0)

试试这个 - &gt;

$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();

答案 3 :(得分:-1)

不确定,但我认为PDO :: bindValue / bindParam适用于变量引用。您不能将静态值设置为参数。 尝试替换

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

通过

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

你也忘记了&#34; AND&#34;您的条件之间的关键字

答案 4 :(得分:-2)

您在语句中缺少ANDOR关键字。此外,LIMITreserved keyword,如果您不想这样做,则需要对其进行反引号或将其重命名为其他内容。

答案 5 :(得分:-4)

$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();