PHP database operation base class based on Singleton mode, php example schema database _php tutorial

Source: Internet
Author: User

PHP database operation base class based on Singleton mode, php example schema database


In this paper, the base class of database operation based on a singleton pattern is described in PHP. Share to everyone for your reference, as follows:

Configuration file:

<?php$db = Array (    ' host ' = ' localhost ',    ' user ' = ' root ',    ' password ' = ' = ', '    database ' = > ' Test ',)?>

PHP Database base class:

<?phpclass db {public $conn;  public static $sql;  public static $instance =null;    Private Function __construct () {require_once (' db.config.php ');    $this->conn = mysql_connect ($db [' Host '], $db [' User '], $db [' Password '];    if (!mysql_select_db ($db [' database '], $this->conn)) {echo "failed";    };  mysql_query (' Set names UTF8 ', $this->conn);    public static function getinstance () {if (Is_null (self:: $instance)) {self:: $instance = new db;  } return Self:: $instance;    /** * Query Database */Public function Select ($table, $condition =array (), $field = Array ()) {$where = '; if (!empty ($condition)) {foreach ($condition as $k = = $v) {$where. = $k. "      = ' ". $v." ' and "; } $where = ' where '. $where. '    1=1 ';    } $fieldstr = ';      if (!empty ($field)) {foreach ($field as $k = = $v) {$fieldstr. = $v. ', ';    } $fieldstr = RTrim ($fieldstr, ', ');    }else{$fieldstr = ' * '; } self:: $sql = ' Select {$fieldstr} from {$table} {$where} ';   $result =mysql_query (self:: $sql, $this->conn);    $resuleRow = Array ();    $i = 0;      while ($row =mysql_fetch_assoc ($result)) {foreach ($row as $k = $v) {$resuleRow [$i] [$k] = $v;    } $i + +;  } return $resuleRow;    }/** * Add a record */Public function Insert ($table, $data) {$values = ';    $datas = ";      foreach ($data as $k = = $v) {$values. = $k. ', ';    $datas. = "' $v '". ', ';    } $values = RTrim ($values, ', ');    $datas = RTrim ($datas, ', ');    Self:: $sql = ' INSERT into {$table} ({$values}) VALUES ({$datas}) ";    if (mysql_query (self:: $sql)) {return mysql_insert_id ();    }else{return false;   };    }/** * Modify a record */Public Function update ($table, $data, $condition =array ()) {$where = '; if (!empty ($condition)) {foreach ($condition as $k = = $v) {$where. = $k. "      = ' ". $v." ' and "; } $where = ' where '. $where. '    1=1 ';    } $updatastr = '; if (!empty ($data)) {foreach ($data as $k = = $v) {$updatastr. = $k."      = ' ". $v." ', ";    } $updatastr = ' Set '. RTrim ($updatastr, ', ');    } self:: $sql = "Update {$table} {$updatastr} {$where}";  Return mysql_query (self:: $sql);    }/** * Delete records */Public Function Delete ($table, $condition) {$where = '; if (!empty ($condition)) {foreach ($condition as $k = = $v) {$where. = $k. "      = ' ". $v." ' and "; } $where = ' where '. $where. '    1=1 ';    } self:: $sql = "Delete from {$table} {$where}";   Return mysql_query (self:: $sql);  } public static function Getlastsql () {echo self:: $sql; }} $db = Db::getinstance ();//$list = $db->select (' demo ', Array (' name ' = ' tom ', ' password ' = ' ds '), Array (' name ' , ' password '),//echo $db->insert (' demo ', Array (' name ' = ' home ', ' password ' = ' 123 ')),//echo $db->update ( ' Demo ', Array ("name" = = ' xxx ', "password" = ' 123 '), array (' ID ' =>1)), Echo $db->delete (' demo ', Array (' id ' = > ' 2 ');d b::getlastsql (); echo "
";? >

Read more about PHP operations database related content readers can view this site topic: "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.

Articles you may be interested in:

    • Implementation code sharing of database singleton mode in PHP
    • PHP implementation of a relatively full database operation class
    • PHP Simple operation MySQL Database class
    • PHP implementation of PDO MySQL database operation class
    • The SQLite database connection class implemented by PHP
    • A 2-way example of database design for PHP+MYSQL tree structure (infinite classification)
    • PHP Lightweight database Operation class Medoo Add, delete, modify, query example
    • PHP Database Universal engine Class ADODB configuration usage and instance collection

http://www.bkjia.com/PHPjc/1092844.html www.bkjia.com true http://www.bkjia.com/PHPjc/1092844.html techarticle PHP Database operation base class based on Singleton mode, php example schema database This paper introduces PHP database operation base class based on singleton pattern implementation. Share to everyone for your reference, ...

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