我正在构建一个数据库对象,它将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 "".print_r($row, true).""; $i++; }
基本上我发现的关于这个错误的是它在提供变量时bindParam
出现null
,但$i
显然不是null.你能帮我吗?
编辑:也在运行
var_dump($this->stmt->bindParam($place, $val, $dataType));
在bindParam
方法返回TRUE
.从手册:
返回值
成功时返回TRUE,失败时返回FALSE.
它成功但没有绑定参数??? 我觉得我的大脑很快就会爆炸.