【发布时间】:2017-05-03 13:11:44
【问题描述】:
index.php:
$db = new Db();
$param = [':name' => 'Alex'];
$data = $db->sql('select * from test where name = :name',$param)->query();
var_dump($data);
并得到错误:
Fatal error: Uncaught Error: Call to a member function fetchAll() on boolean
数据库.php
public function sql($sql,array $params = null)
{
$sql = $this->connection->prepare($sql);
if($params){
foreach ($params as $key => $param) {
$sql->bindParam($key, $param);
}
}
$this->statement = $sql;
return $this;
}
public function query($type = 1)
{
$statement = $this->statement->execute();
return ($type == 1) ? $statement->fetchAll(static::$DB_FETCH) : $statement->fetch(static::$DB_FETCH);
}
如果我在sql()方法中运行,execute()和fetch()里面的数据,确实可以得到数据,但是把execute()和fetch()放到query()方法中,得到错误信息,有什么想法吗? ;
【问题讨论】:
-
false表示您的查询失败,使用php.net/manual/en/pdostatement.errorinfo.php 查看错误。 -
@u_mulder 我 var_dump(statement) 并得到了 ture ./Applications/MAMP/htdocs/Test/Program/Component/Db.php:60:boolean true
-
使用我评论中的功能,请
-
/Applications/MAMP/htdocs/Test/Program/Component/Db.php:61: array (size=3) 0 => string '00000' (length=5) 1 => null 2 => 空