<?phpclass Db{ private $dbConfig=[ 》 gt;'localhost', 'port'=>'3306', 與 ;'root', 'pass'=>'root', 'dbname'=>'edu', ]; //單例模式 private static $instance = null; private static $instance = null; public $insertID = null; public $num1 = null; ///資料庫的連線 private $conn = null; private function __construct($params) 1 { 初始化fig, $params); //連結資料庫 $this->connect(); } private function __clone() { // TODO: Implement 10) stance($params=[]) { if(!self::$instance instanceof self){ self::$instance = new self($params); } return self::$遠 $dsn="{$this->dbConfig['db']} :host={$this->dbConfig['host']}; port={$this->dbConfig['port']};dbname={$this->dbConfig['dbname']} ={$this->dbConfig['charset']}"; //建立pdo物件 $ ;dbConfig['pass']);catch (PDOException $e) { die('資料庫連線失敗'.$e->getMessage()); } $num = $this->conn->exec($ sql); if($num>0) { if(null !== $this->, $this->insertID = $this->conn->lastInsertID() ; } $this->num1= $num; } else{ 0 //0 是錯誤識別碼 1 是錯誤代碼2 是錯誤訊息 print '操作失敗'.$error[0].':'.$error[1].':'.$error[2]; } } public function fetch($sql) $sql)->fetch(PDO::FETCH_ASSOC); } public function fetchALl($sql) { return $this->conn->query); }}
pdo物件沒有取得成功,呼叫了一個物件成員方法fetch, 檢查連線參數