当我尝试执行以下代码时,我一直收到一个空数组:
$this->DB->prepare('SELECT * FROM Articles WHERE Author = :username AND Timestamp < :timestamp ORDER BY Timestamp DESC LIMIT :limit;');
$this->DB->bind(':username', $username);
$this->DB->bind(':timestamp', $start);
$this->DB->bind(':limit', $numberOfResults, \PDO::PARAM_INT);
$data = $this->DB->execute();
这些是bind
,prepare
和execute
函数:
public function bind($placeholder, $value, $type = \PDO::PARAM_STR) {
$this->preparedQuery->bindParam($placeholder, $value, $type);
}
public function prepare($query) {
if(!$this->preparedQuery = $this->connection->prepare($query)) {
print_r($this->connection->errorInfo());
die();
}
}
public function execute() {
if(!$this->preparedQuery->execute()) {
print_r($this->preparedQuery->errorInfo());
die();
}
return $this->preparedQuery->fetchAll(\PDO::FETCH_ASSOC);
}
我还尝试在 MySQL Workbench 中手动执行查询,并且它完全正常工作。
修改
这是查询的样子:
SELECT * FROM Articles WHERE Author = "TestUser" AND Timestamp < "9000-12-31 23:59:59" ORDER BY Timestamp DESC LIMIT 10;
编辑2:
我刚刚发现,如果删除Timestamp
条件,则查询会正确返回值。
答案 0 :(得分:0)
我终于找到了解决方案。
感谢编辑2 我意识到,就像我90%的错误一样,这是一个愚蠢的错误。
$start
变量实际上是空的,即使代码在函数声明中已正确初始化。这导致将一个空字符串传递给bindParam()
函数。
个人评论:我是个白痴。