我有一个函数,用于查询我的数据库:为什么PHP PDO bindValue没有绑定?
public function query($sql, $params = array()) {
// reset error back to false
$this->_error = false;
// check query to prepare
if($this->_query = $this->_pdo->prepare($sql)) {
$x = 1;
if(count($params)) {
foreach($params as $param) {
$this->_query->bindValue($x, $param);
$x++;
}
print_r($this->_pdo->errorInfo());
}
// check query execution
if ($this->_query->execute()) {
$this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
$this->_count = $this->_query->rowCount();
} else {
// error
$this->_error = true;
}
}
return $this;
}
但没有工作的东西。行$this->_query->bindValue($x, $param);
没有做它应该做的事情。没有约束力发生。带参数的数组抛入函数似乎很好。 $sql
声明也很好,并且正在返回:
INSERT INTO responders (`username`, `password`, `salt`) VALUES (?, ?, ?)
对于使用绑定方法应该是完美的。但是在通过foreach循环之后,没有什么改变,_query var仍然是一样的。因此根本不会进入数据库。我也树使用errorInfo和回来:
Array ([0] => 00000 [1] => [2] =>)
任何人都可以给我一个线索我在这里失踪?
你懒得检查bindValue的返回值()调用? –
@TheFrost:那会有什么好处呢?你会将其改为平等测试,并且准备好的陈述将会丢失。例如你主张进一步打破剧本。 –
有趣的是,bindValue()调用的返回值是:1 – konturgestaltung