第77行说非对象调用函数fetch()那位大牛解释一下 实在找不到
问题描述
<?phpclass Db{ private $dbConfig=['db'=>'mysql','host'=>'localhost','port'=>'3306','user'=>'root','pass'=>'root','charset'=>'utf8','dbname'=>'edu',]; //单例模式 private static $instance = null; public $insertID = null; public $num1 = null; ///数据库的连接 private $conn = null; private function __construct($params) {//初始化参数array_merge($this->dbConfig, $params);//连接数据库$this->connect(); } private function __clone() {// TODO: Implement __clone() method. } public static function getInstance($params=[]) {if(!self::$instance instanceof self){ self::$instance = new self($params);}return self::$instance; } private function connect() {try {$dsn="{$this->dbConfig['db']}:host={$this->dbConfig['host']};port={$this->dbConfig['port']};dbname={$this->dbConfig['dbname']};charset={$this->dbConfig['charset']}";//创建pdo对象$this->conn= new PDO($dsn,$this->dbConfig['user'],$this->dbConfig['pass']); //// $this->conn->query("SET NAMES {$this->dbConfig['charset']}");}catch (PDOException $e){ die('数据库连接失败'.$e->getMessage());} } public function exec($sql) {$num = $this->conn->exec($sql);if($num>0){ if(null !== $this->conn->lastInsertID()) {$this->insertID = $this->conn->lastInsertID(); } $this->num1= $num;}else{ $error = $this->conn->errorInfo(); //0 是错误标识符 1 是错误代码 2 是错误信息 print '操作失败'.$error[0].':'.$error[1].':'.$error[2];} } public function fetch($sql) {return $this->conn->query($sql)->fetch(PDO::FETCH_ASSOC); } public function fetchALl($sql) {return $this->conn->query($sql)->fetch(PDO::FETCH_ASSOC);; }}
问题解答
回答1:pdo对象没有获取成功,调用了一个对象成员方法fetch, 检查连接参数.
相关文章:
1. docker start -a dockername 老是卡住,什么情况?2. java内存模型的happens-before语义顺序问题3. java - 并发操作下关于队列的疑问?4. 编程 - java 为什么没有静态方法接口,有没有哪门语言有静态方法接口。5. objective-c - iOS开发使用什么对html进行代码高亮6. linux - 阿里云服务器(centos)中svn同步web目录的问题?7. android - as添加依赖时一直是gradle:download状态8. :not 选择器 无效果 原因何在?9. mysql插入文本如果是个sql语句就报错了10. 在html文件的目录下输入代码按回车后显示这个,哪位大佬帮帮我 呀
