使用PDO :: bindParam时,我的查询不返回任何结果

时间:2013-06-13 10:53:03

标签: php pdo

我正在进行以下查询。如果我将数字直接放入查询中,查询将返回大量结果...现在,如果我使用bindParam传递值,则没有结果。

我已经测试过传递的值是否有值,回显显示它们是......所以我不知道为什么会发生这种情况

有谁能告诉我我做错了什么?

 public function searchWithPagination( $startPage = 0, $numberResultsPerPage = 10 ) {

$q = $this->db->prepare( 'SELECT * FROM ecm LIMIT :startpage, :numberresultsperpage' );
$q->bindParam(':startpage', $startPage);
$q->bindParam(':numberresultsperpage', $numberResultsPerPage);
$q->execute();
echo $numberResultsPerPage . $startPage ;
$f = $q->fetchAll();
var_dump($f);
}

编辑:尝试PDO::PARAM_INT仍然无法正常工作

4 个答案:

答案 0 :(得分:0)

限制参数必须绑定为整数,默认绑定为字符串。

$q->bindParam(':startpage', $startPage, PDO::PARAM_INT);
$q->bindParam(':numberresultsperpage', $numberResultsPerPage, PDO::PARAM_INT);

答案 1 :(得分:0)

正如另一个问题/答案所述:

How to apply bindValue method in LIMIT clause?

你需要将params显式绑定为INT,你也应该将它们转换为整数。

$q->bindParam(':numberresultsperpage', (int)$numberResultsPerPage, PDO::PARAM_INT);

答案 2 :(得分:0)

尝试使用bindValue而不是bindParam。在PHP手册(php.net/manual/en/pdostatement.bindvalue.php)中的用户提交的注释中,有一个关于bindParam通过引用传递的注释,而bindValue没有。

答案 3 :(得分:0)

问题在于你的问题。

在实际运行的代码中,您将绑定常量值

$q->bindParam(':startpage', 0);
$q->bindParam(':numberresultsperpage', 10);

导致你提到的错误:

Cannot pass parameter 2 by reference

但是在你发布的代码中你是绑定变量

$q->bindParam(':startpage', $startPage);
$q->bindParam(':numberresultsperpage', $numberResultsPerPage);
如果使用PDO::PARAM_INT或者关闭仿真代码,

可以正常工作。

相关问题