為什么說非對(duì)象調(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對(duì)象$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)識(shí)符 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對(duì)象沒有獲取成功,調(diào)用了一個(gè)對(duì)象成員方法fetch, 檢查連接參數(shù)
相關(guān)文章:
1. MySQL中無法修改字段名的疑問2. angular.js - angularjs的自定義過濾器如何給文字加顏色?3. angular.js - angular內(nèi)容過長展開收起效果4. javascript - 如何讓移動(dòng)端網(wǎng)頁的輸入框固定在底部?5. 請(qǐng)教各位大佬,瀏覽器點(diǎn) 提交實(shí)例為什么沒有反應(yīng)6. 大家好,請(qǐng)問在python腳本中怎么用virtualenv激活指定的環(huán)境?7. javascript - 微信小程序封裝定位問題(封裝異步并可能多次請(qǐng)求)8. python的前景到底有大?如果不考慮數(shù)據(jù)挖掘,機(jī)器學(xué)習(xí)這塊?9. android - QQ物聯(lián),視頻通話10. javascript - 微信小程序限制加載個(gè)數(shù)
