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.