【发布时间】:2013-12-07 02:14:29
【问题描述】:
我正在构建一个数据库对象,它将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。
成功了但是没有绑定参数???我觉得我的大脑快要爆炸了。
【问题讨论】:
-
如果你把
var_dump($val)放在你定义的bindParam()里面会发生什么?如果你把var_dump($this->stmt)? -
@FranciscoPresencia
int(0)object(PDOStatement)#6 (1) { ["queryString"]=> string(29) "SELECT * FROM users LIMIT ?,1" }我真的很讨厌php对我这样做.. -
我的想法已经不多了,请尝试在您的函数
public function bindParam中使用bindValue()而不是bindParam()以查看是否有变化。从文档中,与 PDOStatement::bindValue() 不同,该变量被绑定为引用,并且只会在调用 PDOStatement::execute() 时进行评估。 但我不确定如果它会改变任何东西。 -
@FranciscoPresencia 即使错误消息保持不变,至少可以改变..
Q_Q -
调试器是你的朋友。