abstract:<?php // 創(chuàng)建數(shù)據(jù)庫的單利模式 2019-04-20 class Connsql { // 把 構(gòu)造函數(shù),克隆函數(shù) 私有化 private function __construct()
<?php // 創(chuàng)建數(shù)據(jù)庫的單利模式 2019-04-20 class Connsql { // 把 構(gòu)造函數(shù),克隆函數(shù) 私有化 private function __construct(){} private function __clone(){} // 添加受保護(hù)的私有屬性 protected static $Link = null; // 創(chuàng)建靜態(tài)方法 public static function Linksql($type,$host,$dbname,$user,$pwd) { // 靜態(tài)屬性是否為空null,為空則創(chuàng)建數(shù)據(jù)庫對(duì)象 if(is_null(static::$Link)) { $dsn = "{$type}:host={$host};dbname={$dbname}"; static::$Link = new PDO($dsn,$user,$pwd); } return static::$Link; } } // 創(chuàng)建pdo 對(duì)象 $pdo = Connsql::Linksql('mysql','127.0.0.1','php_edu','root','root'); // 查詢SQL $sql = "SELECT * FROM `users`"; // sql預(yù)處理 $stmt = $pdo->prepare($sql); // 執(zhí)行預(yù)處理 if($stmt->execute()) { //獲取結(jié)果集 $res = $stmt->fetchall(); echo '<pre>'; print_r($res); } else { // 輸出錯(cuò)誤 print_r($stmt->errorInfo()); }
Correcting teacher:天蓬老師Correction time:2019-04-22 09:40:48
Teacher's summary:如果一個(gè)類, 僅允許實(shí)例化一次, 那么就要從系統(tǒng)收回類實(shí)例化的權(quán)限, 交給用戶決定 .... , 由用戶決定 什么時(shí)候?qū)嵗? 以及如何進(jìn)行....