PHP Simple database Operation class example "support additions and deletions and chain operation" _php tips

Source: Internet
Author: User

This example describes the PHP simple database operations class. Share to everyone for your reference, specific as follows:

In the development of the project, the database is the essential thing. But many times it is a headache for the complexity of SQL statements in the database. Provide a database operation class (model models) that I use for your use. Support to add, delete, change, check, support chain operation, code less than 100 lines, very small and convenient, very suitable for the rapid deployment of small projects use.

/** * * @Authot: Summer * * @e-mail:wenghang1228@me.com * * @Data: 2015-02-06 * * @Project: Database operation class Model * * @Version: 1.0
  * @ Copyright: Summer Wind ———— record the summer of the pursuit of technology and the love of Life * @ URL: http://www.xtwind.com **/class model{public $field;
  Public $tabname;
  Public $where;
  Public $order;
  Public $limit;
    constructor, link the database, assign a value to the table function __construct ($tabname) {mysql_connect (Host,user,password);
    mysql_select_db (dbname);
    mysql_query ("Set names UTF8");
  $this->tabname= $tabname;
    }//Combo field function field ($field) {$this-> $field = $field;
  return $this;
    }//combination where Condition function where ($where) {$this->where= "where". $where;
  return $this;
    }//Combo order sort condition function ($order) {$this->order= ' orders by '. $order;
  return $this;
    }//combination limit limit number function limit ($limit) {$this->limit= "limit". $limit;
  return $this; }//Combine and Execute SELECT statement function select ($all = "") {if ($all) {$sql = ' select {$all} from {$this->tabname} order B Y ID"; }else{$sql = "SELECT {$this->filed} from {$this->tabname} {$this->where} {$this->order} {$this->limit}"
    ";
    $rst =mysql_query ($sql);
    while ($row =mysql_fetch_assoc ($rst)) {$rows []= $row;
  return $rows; }//Group and Execute INSERT statement function Insert ($post) {//Database insert operation, receive array foreach ($post as $key => $value) {$keys []= $key
      ;
    $vals []= "". $valu. "'";
    $keyStr =join (",", $keys);
    $valStr =join (",", $vals);
    $sql = "INSERT INTO {$this->tabname} ($KEYSTR) values ()";
    if (mysql_query ($sql)) {return mysql_insert_id ();
    }else{return false;
    }//Group and Execute Delect statement function Delect () {$sql = "Delect from {$this->tabname} {$this->where}";
    if (mysql_query ($sql)) {return mysql_affected_rows ();
    }else{return false; }//Group and Execute Updata statement function Update ($post) {foreach ($psot as $key => $value) {$sets []=] {$key}= ' {$val}
    '";
  $setStr =join (",", $sets);  $sql = "Update {$this->tabname} set {$setStr} {$this->where}";
    if (mysql_query ($sql)) {return mysql_affected_rows ();
    }else{return false; Take a row of data from the table function find () {if ($this->order) {$sql = ' select * from {$this->tabname} {$this
    ; Order} limit 1 ";
    }else{$sql = "Select * from {$this->tabname} ORDER by ID limit 1";
    $rst =mysql_query ($sql);
    while ($row =mysql_fetch_assoc ($rst)) {$rows []= $row;
  return $rows;
    //Gets the total number of rows () {$sql = "SELECT count (*) from {$this->tabname}";
    $rst =mysql_query ($sql);
      if ($rst) {$row =mysql_fetch_row ($rst);
    return $row [0];
    }else{return false;
//model Class object Factory function M ($tabname) {return new Model ($tabname);} define ("HOST", "localhost");
Define ("USER", "root");
Define ("PASSWORD", "123456");
Define ("dbname", "Test") $user =new Model ("user"); $user->field ("Id,name")->where ("id=3")->order ("ID DESC ")->limit (" 3 ")->select ();

 

Full instance code click here to download the site .

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.

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.