PHP implementation of a simple database operation class (modified version)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.