單例模式實作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
相關文章

10 Mar 2025
本文展示了在PHP中實現數據庫交易管理中的工作單位。 它使用單位工程類來管理單個事務內的多個數據庫操作,從而確保原子能。 文章涵蓋除了

07 Dec 2024
JavaScript 中的單例模式:實作簡單性和清潔性在 JavaScript 中,可以透過多種方式實現單例模式...

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


熱工具

熱門文章
刺客信條陰影:貝殼謎語解決方案
28 Mar 2025
手游攻略
Windows 11 KB5054979中的新功能以及如何解決更新問題
03 Apr 2025
故障排查
在哪裡可以找到原子中的起重機控制鑰匙卡
28 Mar 2025
手游攻略
如何修復KB5055523無法在Windows 11中安裝?
10 Apr 2025
故障排查
Inzoi:如何申請學校和大學
02 Apr 2025
手游攻略