PHP Database Operations model class (using __call method) _php Tips

Source: Internet
Author: User

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

The database operation class uses the __call () method to achieve the lookup function of the data.

The code is as follows:

<?php/* Author: Shyhero * * Define ("HOSTNAME", "127.0.0.1");
Define ("USERNAME", "root");
Define ("PASSWORD", "");
Define ("Dataname", "class");
    Class model{private $link;
    Private $tableName;
    Private $zd;
      Private $method = Array ("where" => "", "Order" => "", "Limit" => "", "group" => "",
    "Having" => "");
      Public function __construct ($tableName) {$this-> = $tableName;
        try{$this-> link = mysqli_connect (hostname,username,password,dataname);
      Mysqli_set_charset ($this-> link, "UTF8");
      }catch (Exception $e) {echo "database connection failed";
    } $this-> desc ();
    The Public Function __destruct () {mysqli_close ($this-> link);
      The Public Function desc () {$sql = "desc {$this-> tablename};";
      $res = mysqli_query ($this-> link, $sql);
      $arr = Mysqli_fetch_all ($res, MYSQLI_ASSOC);
        for ($i = 0; $i < count ($arr); $i + +) {$BRR [] = $arr [$i] [' Field '];
      $this-> ZD = $BRR;
    return $BRR;
      The Public Function __call ($name, $value) {$name = Strtolower ($name); if (Array_key_exists ($name, $this-> method)) {if ($name = = ' order ') {$this-> method[' "] =" or
        Der By ". $value [0];
        }elseif ($name = = ' Group ') {$this-> method[' group ' = ' GROUP by '. $value [0];
        }else{$this-> method[$name] = "{$name}". $value [0];
      }}else{Return "The method isn't found!";
    return $this; Public Function Method () {return "{$this-> method[' where '} {$this-> method[' Order '}} {$this-> me thod[' limit ']} {$this-> method[' group '} {$this-> ' having '};
    "; Public function Find ($a = "*") {if (In_array ("{$a}", $this-> zd) | | $a = = "*") {$sql = ' select {$a} fr
      Om {$this-> tablename} {$this-> method ()} "; }else{$sql = "Select * FROM {$this-> tablename} ";
      }//return $sql;
      $res = mysqli_query ($this-> link, $sql);
      $arr = Mysqli_fetch_all ($res, MYSQLI_ASSOC);
    return $arr;

 }
}

Usage examples:

<?php
  function __autoload ($className) {
    require ($className. "). Class.php ");
  }
  $a = new Model ("Stu");
  $a-> where ("name = ' Zhu '")->limit ("5,10");
  Var_dump ($a-> find ("name"));

More interested in PHP related content readers can view the site topics: "Php+mysql Database Operation Introduction Tutorial", "PHP based on PDO Operation Database Skills Summary", "PHP+MONGODB Database Operation Skills Encyclopedia", "php+ Oracle Database Programming Skills Summary, "PHP+MSSQL Database Programming Skills Summary", "Php+redis Database Programming Skills Summary", "PHP+MYSQLI Database Programming Skills 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.