<?phpclass Db{ private $dbConfig=[ ;' 6 'pass'=>'root', 'charset'=>' utf8', 'dbname'=>'edu', ]; //Single case $instance private = null; public $num1 = null; private function __construct($params) { //Initialization parameters array_merge($this->dbConfig, $params); //Sambung ke pangkalan data $this->connect( } private function __clone() { // TODO : Laksanakan kaedah __clone() } public static function getInstance($params=[]) { if(!self::$instance instanceof self) { self::$instance = new self($params return self::$ instance; fungsi peribadi connect() { $dsn="{$this->dbConfig['d b']}:host = {$ this-& gt; dbconfig ['host']}; port = {$ this- & gt; dbconfig ['port']}; dbname = {$ this- & gt; dbconfig ['dbname']}; C Harset = { $this->dbConfig['charset']}"; 'lulus ']); Tangkap (pdoexception $ E) {DIE ('kegagalan sambungan pangkalan data'. $ e- & gt; getMessage ());} eksekutif fungsi awam ($ SQL) {$ NUM = $ ini- & gt; ; EC ($ sql); if($num>0) if(null !== $this->conn->lastInsertID()) { $this->insertID = $this->conn -> lastInsertID() ; Cetakan Mesej 'Operasi gagal '.$error[0].':'.$error[1].':'.$error[2]; ambil ($sql) . $sql)-> fetch(PDO::FETCH_ASSOC); } public function fetchALl($sql) { return $this->conn->query($sql)->fetch(PDO::FETCH_ASSOC);;
Objek pdo tidak berjaya diperolehi Pengambilan kaedah ahli objek telah dipanggil untuk menyemak parameter sambungan