使用bindValue()的PDO查询方法似乎不起作用

Kid*_*ond 2 php mysql oop data-binding pdo

它永远不会起作用。当我对 sql 查询进行 var 转储时,我发现问号仍然在其中。这意味着这些值还没有被正确绑定?

我不明白为什么它不约束价值观。

有人可以帮我吗?

PHP

$ruleValue = "value1";
$input = "value2";
$inputValue = "value3";

$this->_db->query('SELECT * FROM ? WHERE ? = ?', array($ruleValue, $input, $inputValue));
Run Code Online (Sandbox Code Playgroud)

方法

public function query($sql, $params = array()) {
    $this->_error = false;

    if($this->_query = $this->_pdo->prepare($sql)) {
        $x = 1;
        if(count($params)) {
            foreach($params as $param) {
                $this->_query->bindValue($x, $param);
                $x++;
            }
        }

        if($this->_query->execute()) {
            $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
            $this->_count = $this->_query->rowCount();
        } else {
            $this->_error = true;
        }
        var_dump($this->_query);
    }

    return $this;
}
Run Code Online (Sandbox Code Playgroud)

变量转储

object(PDOStatement)#5 (1) { ["queryString"]=> string(27) "SELECT * FROM ? WHERE ? = ?" }
Run Code Online (Sandbox Code Playgroud)

You*_*nse 5

你的代码:

$ruleValue = "value1";
$input = "value2";
$inputValue = "value3";

$this->_db->query('SELECT * FROM ? WHERE ? = ?', array($ruleValue, $input, $inputValue)
Run Code Online (Sandbox Code Playgroud)

4 行
不安全的
保存状态 -> 你自己挖的一个巨大陷阱
永远不会起作用

常规PDO

$stmt = $this->db->prepare('SELECT * FROM value1 WHERE value2 = ?')
$stmt->execute([$value3]);
$results = $stmt->fetchAll();
Run Code Online (Sandbox Code Playgroud)

3 行
安全
无状态工作

结论:摆脱这个恶意函数并使用原始 PDO