第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. html5 - 最近在自學react 求一個react表單提交的例子2. 老師您好!我有一個問題、3. javascript - jq 上傳圖片成功后添加一個新的上傳框時出現問題4. Python爬蟲如何爬取span和span中間的內容并分別存入字典里?5. mysql - 千萬數據 分頁,當偏移量 原來越大時,怎么優化速度6. javascript - 微信內置瀏覽器的ua是多少?7. jquery清除input type為password?8. python - angular route 與 django urls 沖突怎么解決?9. 我和老師的代碼對照了幾遍沒發現問題,但是瀏覽器打開就有問題了,求解10. 使用PHP和MySQL的UNIQUE如何像京東注冊那樣保證用戶名唯一?
