This article introduces the content of the PHP single-mode code, has a certain reference value, now share to everyone, the need for friends can refer to
Singleton mode, a class allows only one object to be instantiated, saving memory.
On the code:
<?phpheader ("Content-type:text/html;charset=utf8");//define the final simple interest of the database Operation class final class Db{private static $obj = null;// Private database configuration information $dbHost;p rivate $dbName;p rivate $dbUser;p rivate $dbPass;p rivate $charset;//Private Constructor method __construct ($conf) {$this, dbhost = $conf [' Dbhost ']; $this-dbUser = $conf [' DbUser ']; $this, dbName = $conf [' d Bname ']; $this-dbpass = $conf [' Dbpass ']; $this, CharSet = $conf [' CharSet ']; $this->connectdb (); $this Selectdb (); $this->setcharset ();} Private Clone Method __clone () {}public static function getinstance ($conf) {if (!self:: $obj instanceof Self) {self:: $obj = new self ($conf);} Return self:: $obj;} Private Connection database method, Private function Connectdb () {if (! @mysql_connect ($this->dbhost, $this->dbuser, $this->dbpass)) {die (' PHP connection mysql error ');}} Private Select Database method Private Function Selectdb () {if (!mysql_select_db ($this->dbname)) {die (' Connection database error ');}} Private Select Character Set method Private function Setcharset () {mysql_set_charset ($this->charset);} Divides the SQL statement into two classes, returns the result set and returns a Boolean value//return Boolean public function exec ($sql) {//Convert SQL statement to lowercase $sql = strtolower ($sql);//Determine if SELECT statement if (substr ($sql, 0,6) = = ' SELECT ' {die (' cannot execute SELECT statement ');} return mysql_query ($sql);} Private Execute SELECT statement (the result set cannot be displayed directly to the front end, it has to be processed back to the object) Private function query ($sql) {//Convert SQL statement to lowercase $sql = strtolower ($sql);// Determines whether the SELECT statement if (substr ($sql, 0,6)! = ' Select ') {die (' Execute only SELECT statement ');} return mysql_query ($sql);} Returns the result set of the query to the front end (returns a) Public function Fetchone ($sql, $type =3) {$res = $this->query ($sql); $types = Array (1 = mysql_ num,2 = mysql_assoc,3 = Mysql_both,);//Returns a record return Mysql_fetch_array ($res, $types [$type]);} Returns the result set of the query to the front end (returns multiple) public function Fetchall ($sql, $type =2) {$res = $this->query ($sql); $types = Array (1 = mysql_ num,2 = mysql_assoc,3 = Mysql_both,); while ($row = Mysql_fetch_array ($res, $types [$type]) {$arr [] = $row;} return $arr;} Public Function GetCount ($sql) {$res = $this->query ($sql); return mysql_num_rows ($res);}}
Test code:
<?phpheader ("Content-type:text/html;charset=utf8");//class Auto-load Spl_autoload_register (function ($className) {$arr = Array ("./libs/{$className}.class.php",), foreach ($arr as $path) {if (file_exists ($path)) {require_once ($path);}}); $arr = Array (' dbhost ' = ' localhost ', ' dbUser ' = ' root ', ' dbpass ' = ' root ', ' dbName ' = ' test ', ' charset ' = ' UTF8 ',); $db = Db::getinstance ($arr); $sql = "SELECT * from account where id = 100;"; Var_dump ($db->fetchone ($sql, 2));