<?phpclass Db{ private $dbConfig=[ 'db'=& ', 》 calhost', 'port'=>'3306', 'root', 'pass'=>'root', 'dbname'=>'edu', ]; //單例模式 private static $instance = null; private static $instance = null; private static $instance = null; public $ insertID = null; public $num1 = null; ///資料庫的連接 private $conn = null; private function __construct($params) { 初始化fig, $params); //連接資料庫 $this->connect(); } private function __clone() { // TODO:) {為 ance($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-> {$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, 檢查連線參數.