單例模式實作mysql的PHP類
<?php defined('ACC')||exit('Access Denied'); // 封装mysql操作类,包括连接功能,及查询功能. class mysql extends absdb{ protected static $ins = null; protected $host; // 主机名 protected $user; // 用户名 protected $passwd; // 密码 protected $db; // 数据库名 protected $port; // 端口 protected $conn = null; // 在内部操作,获得一个对象 public static function getIns() { if(self::$ins === null) { self::$ins = new self(); } $conf = conf::getIns(); self::$ins->host = $conf->host; self::$ins->user = $conf->user; self::$ins->passwd = $conf->pwd; self::$ins->db = $conf->db; self::$ins->port = $conf->port; self::$ins->connect(); self::$ins->select_db(); self::$ins->setChar(); return self::$ins; } // 不让外部做new操作, protected function __construct() { } // 连接数据库 public function connect() { $this->conn = @mysql_connect($this->host,$this->user,$this->passwd,$this->port); if(!$this->conn) { $error = new Exception('数据库连不上',9); throw $error; } } // 发送sql查询 public function query($sql) { $rs = mysql_query($sql,$this->conn); if(!$rs) { log::write($sql); } return $rs; }
這是一個單例模式實作mysql的PHP類,需要的朋友可以下載使用。
免責聲明
本站所有資源皆由網友貢獻或各大下載網站轉載。請自行檢查軟體的完整性!本站所有資源僅供學習參考。請不要將它們用於商業目的。否則,一切後果都由您負責!如有侵權,請聯絡我們刪除。聯絡方式:admin@php.cn
相關文章

19Dec2024
了解 Javascript 靜態函數表達式:GameData 案例在 Javascript 中,使用 new 關鍵字的函數表達式不...

29Nov2024
簡單的 PHP Post-Redirect-Get (PRG) 程式碼範例 在 PHP 中實作 Post-Redirect-Get (PRG) 非常簡單。這是一個簡化的...


Hot Tools

熱門文章
丟失記錄:Bloom&Rage -Corey和Dylan的鎖組合
19Feb2025手游攻略
王國來:拯救2-婚禮要買的衣服
05Feb2025手游攻略
丟失的記錄:Bloom&Rage-成就/獎杯指南
19Feb2025手游攻略
王國來:拯救2-如何獲得Comfrey
07Feb2025手游攻略
Hello Kitty Island冒險:重新啟動機器人任務演練
16Feb2025手游攻略