来自函数变量的bindParam的PDO查询问题

时间:2017-09-27 13:51:58

标签: php mysql pdo query-builder

我遇到了按函数查询和使用变量绑定的问题。

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

0 个答案:

没有答案