為什么說非對象調(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 是錯誤標(biāo)識符 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對象沒有獲取成功,調(diào)用了一個對象成員方法fetch, 檢查連接參數(shù)
相關(guān)文章:
1. python - 爬取微信公眾號文章需要輸入驗證碼問題2. mysql - 對單表大量數(shù)據(jù)進(jìn)行報表匯總有什么高效的方法3. fetch_field_direct()報錯4. 為什么點擊登陸沒反應(yīng)5. 數(shù)據(jù)庫設(shè)計 - 社交應(yīng)用的mysql表主鍵該怎么定義?6. mysql - 10g數(shù)據(jù)庫如何遷移7. 在視圖里面寫php原生標(biāo)簽不是要迫不得已的情況才寫嗎8. mysql多表聯(lián)合查詢優(yōu)化的問題9. node.js - session怎么存到cookie,然后服務(wù)器重啟后還能獲取。數(shù)據(jù)庫不用mongodb或redis,數(shù)據(jù)庫是mysql10. javascript - vue引入微信jssdk 配置在哪個生命周期調(diào)取接口配置?
