PHP based on single case pattern encapsulation MySQL class complete instance _php skill

Source: Internet
Author: User
Tags trim

The example in this article describes the PHP based on a single case pattern encapsulation MySQL class. Share to everyone for your reference, specific as follows:

Class:

<?php header ("Content-type:text/html;charset=utf-8"); Encapsulate a class///* Mastering the necessary conditions for a singleton pattern (1) Private construction method-to prevent the use of the new keyword to instantiate object (2) Private member properties outside the class-to prevent the introduction of the property of this host object outside the Class (3)
  Private cloning Method-in order to prevent the static method of genetic another object (4) Public by the clone outside of the class-to allow the user to instantiate the operation of the object * * Class db{//Three private total//private static property private static $dbcon =false;
   Private construction Method Private function __construct () {$dbcon = @mysql_connect ("localhost", "root", "root");
   mysql_select_db ("Small2", $dbcon) or Die ("Mysql_connect error");
  mysql_query ("Set names UTF8"); }//Private cloning method Private Function __clone () {}//public static Method-Common statics function getintance () {if (self:: $dbcon ==false)
   {self:: $dbcon =new self;
  Return self:: $dbcon;
    ///Print data public function p ($arr) {echo <pre>;
    Print_r ($arr);
  echo "</pre>";
    The Public Function V ($arr) {echo <pre>;
    Var_dump ($arr);
  echo "</pre>";
   ///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);
   //Get 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); //Get a record, the precondition gets a record from the resource the public function Getformsource ($query, $type = "Assoc") {if (!in_array ($type, Array ("Assoc", "ar
  Ray "," 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 the last Record ID public function Getinsertid () {return mysql_insert_id (); /** * Defines methods for adding data * @param string $table table name * @param string Orarray $data [data] * @return INT recently added ID
   */Public Function Insert ($table, $data) {//traverse array, 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 corresponding value of each field S field $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 to do produces the ID value return mysql_insert_id (); * * * Delete a data method * @param1 $table, $where =array (' id ' => ' 1 ') Table name condition * @return Affected number of rows/public function Deleteon E ($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 condition * @return Affected number of rows/public function deletealL ($table, $where) {if (Is_array ($where)) {foreach ($where as $key => $val) {if (Is_array ($val)) {
        $condition = $key. ' 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] *
  @return [Type]/Public function update ($table, $data, $where) {//traverse 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 ();

 }}?>

Test:

MySQL test
//$db =db::getintance ();
Var_dump ($db);
/* $sql = "SELECT * from acticle";
$list = $db->getall ($sql);
$db->p ($list); * *
$sql = "SELECT * from acticle where acticle_id=95";
$list = $db->getrow ($sql);
$db->p ($list);
* *
* $sql = "Select title from Acticle";
$list = $db->getone ($sql);
$db->p ($list); *
//$list = $db->insert ("Users", $_post);
$del = $db->deleteone ("Users", "id=26");
$del = $db->deleteall ("Users", "ID in (23,24)");
$up = $db->update ("Users", $_post, "id=27");
$id = $db->getinsertid ();
Print_r ($id);

For more information about PHP interested readers can view the site topics: "Php+mysql database operations to get Started", "PHP basic Grammar Introductory Course", "PHP Operations and Operator Usage Summary", "PHP object-oriented Program Design Introductory Course", "PHP Network Programming Skills Summary", " PHP array Operation tips Encyclopedia, PHP string (String) Usage summary and PHP Common database operation Skills Summary

I hope this article will help you with the PHP program design.

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.