PHP implementation of a simple database operation class implementation of the function:-At the time of instantiation can be set up the connection character set-when the instantiation of the database can be connected-when the instantiation of the default database can be selected-the destruction of the object when the database code as follows:
<?php//Database operation class MySQLdb class mysqldb { //Declaration Properties Private $server;Private $port;Private $username;Private $password; Public $default _db;Private $charset; Public $link;//constructors use arrays as arguments when there are many parameters Public function __construct($params = array()) { Echo ' __construct ',' <br/> ';//Set the connection string to implement the default value setting through the conditional operator $this->server =isset($params[' Server ']) ?$params[' Server '] :' 127.0.0.1 ';$this->username =isset($params[' username ']) ?$params[' username '] :' Root ';$this->password =isset($params[' Password ']) ?$params[' Password '] :' 123456 ';$this->port =isset($params[' Port ']) ?$params[' Port '] :' 3306 ';$this->charset =isset($params[' CharSet ']) ?$params[' CharSet '] :' UTF8 ';$this->default_db =isset($params[' default_db ']) ?$params[' default_db '] :' MyDB ';//Instantiate the object when connecting to the database, selecting the default database, and setting the character set $this->connect_db ();$this->select_db ();$this->set_charset (); }//functions to connect to the database Public function connect_db() { $this->link = mysql_connect ("$this->server: $this->port",$this->username,$this->password);if(!$this->link) {Echo ' database connect failure! '; die(); } }//select default Database Public function select_db() { $bool= mysql_select_db ($this->default_db,$this->link);if(!$bool) {Echo ' Select default_db failure! '; die(); } }//Set character sets Public function set_charset() { $sql="Set names $this->charset";$bool= mysql_query ($sql,$this->link);if(!bool) {Echo ' Set charset failure '; die(); } }//destructor Public function __destruct() { Echo ' __destruct ',' <br/> '; Mysql_close ($this->link); }}
PHP implementation of a simple database operation class (modified version)