我正在构建一个连接的数据库对象 PDO
对象 PDOStatement
对象以便链接可用。基本上我只是把我经常使用的方法,但是 bindParam
给了我一个艰难的时刻。
private $stmt = null;
...
public function prepare($statement,array $driver_options = array()) {
if($this->stmt) throw new \Exception('PDO Statement already prepared, no override!');
$this->stmt = parent::prepare($statement, $driver_options);
return $this;
}
public function bindParam($place, &$val, $dataType){
if(!$this->stmt) throw new \Exception('PDO Statement is empty');
$this->stmt->bindParam($place, $val, $dataType);
return $this;
}
public function execute(array $params = array()){
if(!$this->stmt) throw new \Exception('PDO Statement is empty');
$this->stmt->execute($params);
return $this;
}
public function fetchAll($pdoFetchType){
if(!$this->stmt) throw new \Exception('PDO Statement is empty');
return $this->stmt->fetchAll($pdoFetchType);
}
...
public function getStmt(){
return $this->stmt;
}
public function clearStmt(){
$this->stmt = null;
}
我在这段代码中从标题中得到错误:
$i = 0;
$db->prepare('SELECT * FROM users LIMIT ?,1')->bindParam(1, $i, \PDO::PARAM_INT);
while($row = $db->execute()->fetchAll(\PDO::FETCH_ASSOC)){
echo "<pre>".print_r($row, true)."</pre>";
$i++;
}
基本上我发现的关于这个错误的是它在提供变量时发生 bindParam
是 null
但是 $i
显然不是空的。你能帮我吗?
编辑:还在跑步
var_dump($this->stmt->bindParam($place, $val, $dataType));
在里面 bindParam
方法返回 TRUE
。从手册:
返回值
成功时返回TRUE,失败时返回FALSE。
它成功但没有绑定参数???我觉得我的大脑很快就会爆炸。