為什么說非對象調(diào)用成員函數(shù)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; ///數(shù)據(jù)庫的連接 private $conn = null; private function __construct($params) {//初始化參數(shù)array_merge($this->dbConfig, $params);//連接數(shù)據(jù)庫$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']}";//創(chuàng)建pdo對象$this->conn= new PDO($dsn,$this->dbConfig['user'],$this->dbConfig['pass']); //// $this->conn->query("SET NAMES {$this->dbConfig['charset']}");}catch (PDOException $e){ die('數(shù)據(jù)庫連接失敗'.$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 是錯(cuò)誤標(biāo)識符 1 是錯(cuò)誤代碼 2 是錯(cuò)誤信息 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對象沒有獲取成功,調(diào)用了一個(gè)對象成員方法fetch, 檢查連接參數(shù)
相關(guān)文章:
1. javascript - 百度echarts series數(shù)據(jù)更新問題2. MySQL客戶端吃掉了SQL注解?3. python小白的基礎(chǔ)問題 關(guān)于while循環(huán)的嵌套4. css3 - [CSS] 動(dòng)畫效果 3D翻轉(zhuǎn)bug5. javascript - JS設(shè)置Video視頻對象的currentTime時(shí)出現(xiàn)了問題,IE,Edge,火狐,都可以設(shè)置,反而chrom卻...6. java - 我設(shè)置了cookie的max age,但是cookie依然在關(guān)閉游覽器后消失了7. java固定鍵值轉(zhuǎn)換,使用枚舉實(shí)現(xiàn)字典?8. php自學(xué)從哪里開始?9. phpstady在win10上運(yùn)行10. 求大神幫我看看是哪里寫錯(cuò)了 感謝細(xì)心解答
