[英]How to access PDO database connection object in another class in php?
I am using PDO in my project, So My PDO connection with database was made successfully but when I access the PDO object in another class system encountered error like following 我在项目中使用PDO,因此我与数据库的PDO连接已成功建立,但是当我在另一个类系统中访问PDO对象时遇到如下错误
Catchable fatal error: Object of class PDO could not be converted to string
可捕获的致命错误:PDO类的对象无法转换为字符串
Following is my database connection class 以下是我的数据库连接类
class DBConnect
{
public static function getDB()
{
try {
$dsn = DTConfig::dbtype.":host=".DTConfig::host.";dbname=".DTConfig::db;
$user = DTConfig::user;
$dbname = DTConfig::password;
$db = new PDO($dsn,$user,$dbname);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
return $db;
}
}
In following code I want to access the PDO object but I got error like "Object of class PDO could not be converted to string on line 5" 在下面的代码中,我想访问PDO对象,但出现类似“ PDO类的对象无法在第5行上转换为字符串”之类的错误。
class Administrator{
function __construct(){
$db = DBConnect::getDB(); //line 5 - This is not working
//echo $db;die;
}
Anybody please suggest me answer Thanks in advance 有人请建议我回答提前谢谢
I just tried it, when I try to echo the PDO object I get the same error. 我只是尝试过,当我尝试回显PDO对象时,我遇到了同样的错误。
Replace 更换
echo $db;
with: 与:
var_dump($db);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.