PHP based on a singleton pattern implementation of MySQL class

Source: Internet
Author: User
Tags php database
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.

The above describes the PHP based on a singleton model of the MySQL class, including the aspects of the content, I hope the PHP tutorial interested in a friend helpful.

  • 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.