本文介绍了PHP PDO类编程:致命错误:在布尔值上调用成员函数fetchAll()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是php类编程的新手,这是我的数据库类.
I am new to the class programming in php ,Here is my database class.
class Database
{
private $_connection;
private static $_instance; //The single instance
private $_host = 'localhost';
private $_username = 'root';
private $_password = '';
private $_database = 'admission_portal';
//connect to database
public function connectDb()
{
try {
$this->_connection = new \PDO("mysql:host=$this->_host;dbname=$this->_database", $this->_username, $this->_password);
/*** echo a message saying we have connected ***/
echo 'Connected to database';
} catch (PDOException $e) {
echo $e->getMessage();
}
}
//run the query
public function run($sql)
{
$result=$this->_connection->prepare($sql);
return $result->execute();
}
}
我将其扩展到核心类以执行一些数据库操作.
I am extending this to core class to do some database operations.
class Core extends Database
{
//get all the universities
public function getData()
{
Database::connectDb();
$sql = 'SELECT * FROM `adm_universities`';
$r=Database::run($sql);
print_r($r->fetchAll(PDO::FETCH_OBJ));
}
}
现在我正在以这种方式调用getData函数.
Now i am invoking getData function like this way.
$db=new Core();
$db->getData();
但是我会得到这个
推荐答案
只需要在run()
方法中返回$result
:
public function run($sql)
{
$result=$this->_connection->prepare($sql);
$result->execute();
return $result;
}
返回$result->execute();
将返回true
,因为execute()
成功.您需要返回$result
的当前状态.
Returning the $result->execute();
is returning true
because the execute()
succeeded. You need to return the current state of $result
.
看看是否可行.
这篇关于PHP PDO类编程:致命错误:在布尔值上调用成员函数fetchAll()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!