A single-instance mode MySQL operation class for PHP encapsulation

Source: Internet
Author: User
PHP package of a single-case mode MySQL operation class in PHP has an important role, this article will explain in detail its related knowledge.

PHP package for a single-case mode MySQL operation class

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 Property $dbcon = false;    2. 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 function __clone () {}//1. The public static method of the common statics function getintance () {if (Self:: $DBCO    N==false) {self:: $dbcon =new self;  } return Self:: $dbcon;       }//Execute statement Public Function query ($sql) {$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 of 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); }   //Get a record, pre-condition gets a record through the resource public function Getformsource ($query, $type = "Assoc") {if (!in_array ($type, Array ("Assoc", "Arra     Y "," 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;  } return $list;  }//Get last Record ID public function Getinsertid () {return mysql_insert_id (); }/** * defines how to add data * @param string $table table name * @param string Orarray $data [data] * @return int the newly added ID */Publ     IC function Insert ($table, $data) {//Iterate through the array to get the value of each field and field $key _str= ';       $v _str= ';      foreach ($data as $key = + $v) {if (empty ($v)) {die ("error");      The value of the//$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, ', '); Determine if 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 of return mysql_insert_id (); }/* * Delete a data method * @param1 $table, $where =array (' id ' = ' 1 ') Table name condition * @return The number of rows affected * */Public Function Deleteone ($tab Le, $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 condition * @return The number of rows affected */Public function deleteAll ($table, $where) { if (Is_array ($where)) {foreach ($where as $key = + $val) {if (Is_array ($val)) {$cond        ition = $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 [condition] * @retur  n [Type] */Public Function update ($table, $data, $where) {//Iterate through the array to get the value of each field and field $str = ';  foreach ($data as $key + $v) {$str. = "$key = ' $v ',";  } $str =rtrim ($str, ', ');  Modify the SQL statement $sql = "Update $table set $str where $where";  $this->query ($sql); Returns the number of rows affected return mysql_affected_rows (); }}

This article details the PHP package of a single-mode MySQL operation related knowledge, more relevant knowledge, please pay attention to the PHP Chinese web.

Related Article

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.