PDO准备好的语句返回空集,查询工作正常 [英] PDO prepared statement returns empty set, Query works fine
本文介绍了PDO准备好的语句返回空集,查询工作正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我下面有执行Mysql查询的PDO函数.它适用于所有查询.
I have below PDO function that executes a Mysql query. Its working correctly for all queries.
public function run($sql, array $params = NULL) {
$statement = $this->pdo->prepare($sql);
if (!is_null($params)) {
foreach ($params as $key) {
$statement->bindParam(":n", $key);
}
}
$statement->execute();
return $statement->fetchAll(PDO::FETCH_CLASS);
}
但是,当我运行以下查询时,它返回一个空集
However when i run the below query its returns an empty set
$variation = $query->run(
"SELECT url, title, sale_price
FROM product
where category_id = :n
and url != :n",
[ $data[0]->category_id, $filePathArray[1] ]
);
当我在mysql客户端中手动运行查询时,它起作用.
Its works when i run the query manually in mysql client.
我尝试过类型转换,但没有喜悦:
I tried type casting the category id but no joy:
(int)$data[0]->category_id
(因为它传递的是字符串而不是Integer)
(since it was passing a string as opposed to Integer)
这是运行函数中$ params的var_dump
Here is the var_dump of $params from within the run function
array(2) {
[0]=> int(1)
[1]=> string(21) "light-resistance-band"
}
推荐答案
您需要添加 不同的命名占位符以绑定相应的值 (不完全相同)>
$variation = $query->run(
"SELECT url, title, sale_price
FROM product
where category_id = :n
and url != :n1",
[ ':n'=>$data[0]->category_id, ':n1'=>$filePathArray[1] ]
);
这篇关于PDO准备好的语句返回空集,查询工作正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文