1<?PHP2 Header(' content-type:text/html; Charset=utf-8 ');3 error_reporting(E_all^e_deprecated);4 //Database Operations Classes5 classmysqldb{6 7 //Properties--Necessary information8 Private $_host;//Host9 Private $_port;//PortTen Private $_user;//User name One Private $_pass;//Password A Private $_charset;//Character Set - Private $_dbname;//default Database - the //Properties--run-time generation - Public $_link;//MySQL Link resource - - Private Static $_instance; + Public Static functionGetInstance ($params=Array()){ - if(!self::$_instanceinstanceof Self) { +Self::$_instance=NewSelf$params); A } at returnSelf::$_instance; - } - Private function__clone () {} - - /* - * Construction Method in * @param $param =array () array associative array requires six property values - */ to Private function__construct ($params=Array()){ + //Initialize Properties - $this->_initparams ($params); the //connecting to the database server * $this-_connect (); $ //Set character setPanax Notoginseng $this-_setcharset (); - //Select default Data the $this-_selectdb (); + } A the /* + * Initialize database server parameters - */ $ Private function_initparams ($params){ $ $this->_host =isset($params[' Host ']?$params[' Host ']: ' 127.0.0.1 ';//This machine - $this->_port =isset($params[' Port ']?$params[' Port ']: ' 3306 ';//3306 - $this->_user =isset($params[' User ']?$params[' User ']: ';//Anonymous User the $this->_pass =isset($params[' Pass '])?$params[' Pass ']: ';//No password - $this->_charset =isset($params[' CharSet '])?$params[' CharSet ']: ' UTF8 ';//UTF8Wuyi $this->_dbname =isset($params[' dbname '])?$params[' dbname ']: ';//indicates that the default database is not selected the } - Wu - /* About * Connect to database server $ */ - Private function_connect () { - //127.0.0.1:3306 - if($link=mysql_connect("$this->_host:$this->_port ",$this->_user,$this-_pass)) { A //Success + $this->_link =$link; the}Else{ - //failed, force script to end, prompt error message $ die("-_-!, failed to connect to the database server, please determine connection options"); the } the } the the /* - * Set the specified character set in */ the Private function_setcharset () { the $sql= "Set names$this->_charset "; About if(mysql_query($sql,$this-_link)) { the //Success the}Else{ the //execution failure gives an error message + Echo‘-_-! SQL execution failed <br/> '; - Echo' The wrong SQL is: ',$sql, ' <br/> '; the EchoThe error code is: ',Mysql_errno($this->_link), ' <br/> ';Bayi Echo' The error message is: ',Mysql_error($this-_link); the die; the } - } - the /* the * Select default database the * If not specified, do not select the */ - Private function_selectdb () { the //determine if you need to select a default database, which indicates no need to prevent 0 the if($this->_dbname!== '){ the //not empty, select database94 $sql= "Use"$this->_dbname ' "; the if(mysql_query($sql,$this-_link)) { the //Success the}Else{98 //execution failure gives an error message About Echo‘-_-! SQL execution failed <br/> '; - Echo' The wrong SQL is: ',$sql, ' <br/> ';101 EchoThe error code is: ',Mysql_errno($this->_link), ' <br/> ';102 Echo' The error message is: ',Mysql_error($this-_link);103 die;104 } the }106 }107 108 109 the /**111 * methods to be executed when serializing the * @return Array Each array element is a property name that needs to be serialized113 * the */ the Public function__sleep () { the return Array(' _host ', ' _post ', ' _user ', ' _pass ', ' _charset ', ' _dbname ');117 }118 119 /* - * is executed when deserializing121 * Re-initialization of work122 */123 Public function__wakeup () {124 //connecting to the database server the $this-_connect ();126 //Setting the connection character set127 $this-_setcharset (); - //Select the default database129 $this-_selectdb (); the }131}
View Code
The Simple MySQLdb class