PHP based on a singleton model of the MySQL class, Phpmysql class
This paper describes the MySQL class implemented by PHP based on a singleton pattern. Share to everyone for your reference, as follows:
<?phpdefined (' ACC ') | | Exit (' Access Denied ');//package MySQL operation class, including connection function, and query function. class MySQL extends absdb{protected static $ins = NULL; protected $host; Host name protected $user; User name protected $passwd; Password protected $db; Database name protected $port; Port protected $conn = NULL; Inside operation, get an object 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; }//Do not let external do new operation, protected function __construct () {}//Connect database Public function connect () {$this->conn = @mysql _connect ($this->host, $this->user, $this->passwd, $this->port); if (! $this->conn) {$error = new Exception (' Database Not connected ', 9); Throw $error; }}//Send SQL query Public function query ($sql) {$rs = mysql_query ($sql, $this->conn); if (! $rs) {log::write ($sql); } return $rs; }//Encapsulation of a GetAll method//parameter: $sql//return: Array,false Public Function GetAll ($sql) {$rs = $this->query ($sql); if (! $rs) {return false; } $list = Array (); while ($row = Mysql_fetch_assoc ($rs)) {$list [] = $row; } return $list; }//Encapsulation of a GetRow method//parameter: $sql//return: Array,false Public Function GetRow ($sql) {$rs = $this->query ($sql); if (! $rs) {return false; } return Mysql_fetch_assoc ($RS); }//Encapsulates a GetOne method,//Parameters: $sql//return: INT,STR (single value) public function GetOne ($sql) {$rs = $this->query ($sql); if (! $rs) {return false; } $tmp = Mysql_fetch_row ($RS); return $tmp [0]; }//Encapsulates a afftect_rows () method//parameter: none//returns int affected number of rows public function affected_rows () {return mysql_affected_rows ($thi S->conn); }//Returns the value of the newly generated auto_increment column publIC function last_id () {return mysql_insert_id ($this->conn); }//Library functions Public Function select_db () {$sql = ' use '. $this->db; return $this->query ($sql); }//sets the function of the character set public functions SetChar () {$sql = ' set names UTF8 '; return $this->query ($sql); }//Automatically generate INSERT statement, UPDATE statement and execute public function Autoexecute ($data, $table, $act = ' Insert ', $where = ') {if ($act = = ' Insert ') {$sql = ' insert INTO '. $table. ' ('; $sql. = Implode (', ', (Array_keys ($data))); $sql. = ') values (\ '; $sql. = Implode ("', '", Array_values ($data)); $sql. = "')"; } else if ($act = = ' Update ') {if (!trim ($where)) {return false; } $sql = ' Update '. $table. ' Set '; foreach ($data as $k = = $v) {$sql. = $k; $sql. = ' = '; $sql. = "'". $v. "',"; } $sql = substr ($sql, 0,-1); $sql. = ' where '; $sql. = $where; } else {return false; }//return $sql; return $this->query ($sql); }}
Read more about PHP database operations related content readers can view the site topics: "Php+mysql Database Operation Primer", "PHP based on PDO Operation Database skills summary" and "PHP common database Operation Skills Summary"
I hope this article is helpful to you in PHP programming.
Articles you may be interested in:
- PHP single-case mode detailed introduction
- A case study of PHP design pattern in single instance mode
- PHP single-state design mode (singleton mode) example
- PHP uses singleton mode to implement log processing class library
- Example of PHP design pattern using a singleton pattern
- PHP Singleton pattern Implementation (objects are created only once)
- PHP package connection MySQL class and usage analysis
http://www.bkjia.com/PHPjc/1089929.html www.bkjia.com true http://www.bkjia.com/PHPjc/1089929.html techarticle PHP based on the implementation of a singleton model MySQL class, Phpmysql class This article describes the PHP based on a single-mode implementation of the MySQL class. Share to everyone for your reference, specifically as follows: phpdefined (' ACC ' ...