我遇到了按函数查询和使用变量绑定的问题。
public function get_where($what, $condition, $thing)
{
$this->statement = "SELECT * FROM $this->table WHERE :what :condition :thing";
$stmt = $this->db->pdo()->prepare($this->statement);
$stmt->bindParam(':what', $what);
$stmt->bindParam(':condition', $condition);
$stmt->bindParam(':thing', $thing);
$stmt->execute();
return $result = $stmt->fetchAll();
}
用法:
$result = $QueryBuilder->table('calendar')
->get_where('MONTH(date)', '=', '9');
var_dump($result);
结果为empty array
正如您所看到的,我将此MONTH(date)
传递给what
,将=
传递为condition
,将9
传递为thing
。
但它不起作用,empty array
但是当我手动更改时
$this->statement = "SELECT * FROM $this->table WHERE :what :condition :thing";
到
$this->statement = "SELECT * FROM $this->table WHERE MONTH(date) = 9";
效果很好。
那么为什么这里错了? :/
另外我可以这样做
$result = $QueryBuilder->table('calendar')
->get_where('1', '=', '1');
我在结果中得到all rows