To master the necessary conditions to meet the singleton pattern----three private and one public.
① private constructor-to prevent the instantiation of an object with the new keyword outside of the class.
② Private member Property-to prevent the introduction of the property of this object from outside the class.
③ private cloning method-to prevent another object from being genetic through clone outside of the class.
④ public static Method-to allow the user to instantiate the object's operation.
DB Class and related table operation;
Class mysql_db{ //1. Private static properties private static $dbcon = false; //2. Private method of construction private function __construct () { $dbconn = @mysql_connect ("localhost", "root", ""); mysql_ select_db ("Test", $dbconn) or die ("Mysql_connect error"); mysql_query (" Set names utf8 "); } //3. Private Cloning Method private function __clone () { } //1. public static methods public Static function getintance () { if (self:: $dbcon ==false) { self:: $dbcon =new self; } return self:: $dbcon; } //EXECUTE statement public function query ($sql)  {&Nbsp; $query =mysql_query ($sql); return $ query; } /** * Query a field * @param * @ Return string or int */ public function getone ($sql) { $query = $this->query ($sql); Return mysql_result ($query, 0); } //gets a row of records,return array one-dimensional array public function getrow ($sql, $type = "Assoc") { $ query= $this->query ($sql); if (!in_array ($type, Array ("Assoc", ' array ', "Row"))) { die ("Mysql_ Query error "); } $funcname = "Mysql_fetch_". $type; return $funcname ($query); } //gets a record, the precondition gets a record through the resource public function getformsource ($query, $type = " Assoc ") { if (!in_array ($type, Array (" Assoc "," Array "," Row ")) ) { die ("Mysql_query error"); } $funcname = "Mysql_fetch_". $type; return $funcname ($query); } //get multiple data, two-dimensional array public function getall ($sql) { $query = $this->query ($sql); $list =array (); while ($r =$ This->getformsource ($query)) { $list []= $r; &nBSP;} return $list; } // Get the last record Id public function getinsertid () { return  MYSQL_INSERT_ID (); } /** * define methods for adding data * @param string $table table name * @param string orarray $ data [data] * @return int latest added id */ Public function insert ($table, $data) { //iterates through an array to get the values of each field and field $key _str= '; $v _str= '; foreach ($data as $key = $v) { if (Empty ($v)) { die ("error");nbsp; } //$ The value of key is the value of one field per field s $key _str.= $key. ', '; $v _str.= "' $v ',"; } $key _str=trim ($key _str, ', '); $v _ Str=trim ($v _str, ', '); //determines whether the data is empty $sql = "insert into $table ($ KEY_STR) values ($v _str) "; $this->query ($sql); //returns the last increment of the operation to generate the ID value return mysql_insert_id (); } /* * Delete a data method * @param1 $table, $where =array (' id ' = ' 1 ') table name condition * @return Number of rows affected */ public function deleteone ($table, $where) { if (Is_array ($where)) { foreach ($where as $key => $val) { $condition = $key. ' = '. $val; } } else { $condition = $where; } $sql = "delete from $table where $condition"; $this->query ($sql); //returns the number of rows affected return mysql_affected_rows (); } /* * Delete multiple data methods * @param1 $table, $where table name conditions * @return Number of rows affected */ public function deleteall ($table, $where) { if (Is_array ($where)) { foreach ($where as $key => $val) { if (Is_array ($val)) { $condition = $key. ' in ('. Implode (', ', $val) . ') '; } else { $condition = $key . ' = ' . $val; } } } else { $condition = $where; } $sql = " delete from $table where $condition "; $this->query ($sql); //returns the number of rows affected return mysql_affected_rows (); } /** * [Modify Operation Description] * @param [type] $table [table name] * @param [type] $data [Data] * @param [type] $where [conditions] * @return [type] * / public function update ($table, $data, $where) { //iterate through the array to get the values of each field and field $str = "; foreach ($data as $key = $v) { $str. = "$key = ' $v ',"; } $str =rtrim ($str, ', '); //Modify SQL statements $sql = "update $table set $str where $ where "; $this->query ($sql); //returns the number of rows affected return mysql _affected_rows (); }}
Instantiating an object:
$db = Mysql_db::getintance (), $list = $db->getall ("Select Field1,field2 from Acticle"), $list = $db->getrow ("Select Field1,field2 from acticle WHERE acticle_id=1018 ");
PHP package for a single-case mode MySQL operation class