本文实例讲述了PHP数据库操作model类。分享给大家供大家参考,具体如下:
该数据库操作类使用__call()方法实现了数据的查找功能。
代码如下:
PHP;">
"","order" => "","limit" => "","group" => "","having" => ""
);
public function __construct($tableName){
$this -> tableName = $tableName;
try{
$this -> link = MysqLi_connect(HOSTNAME,USERNAME,PASSWORD,DatanAME);
MysqLi_set_charset($this -> link,"UTF8");
}catch(Exception $e){
echo "数据库连接失败";
}
$this -> desc();
}
public function __destruct(){
MysqLi_close($this -> link);
}
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;
}
public function __call($name,$value){
$name = strtolower($name);
if(array_key_exists($name,$this -> method)){
if($name == 'order'){
$this -> method['order'] = " order by ".$value[0];
}elseif($name == 'group'){
$this -> method['group'] = " group by ".$value[0];
}else{
$this -> method[$name] = " {$name} ".$value[0];
}
}else{
return "the method is not found!";
}
return $this;
}
public function method(){
return " {$this -> method['where']} {$this -> method['order']} {$this -> method['limit']} {$this -> method['group']} {$this -> method['having']}; ";
}
public function find($a="*"){
if(in_array("{$a}",$this -> zd) || $a == "*"){
$sql = "select {$a} from {$this -> tableName} {$this -> method()} ";
}else{
$sql = "select * from {$this -> tableName}";
}
//return $sql;
$res = MysqLi_query($this -> link,MysqLI_ASSOC);
return $arr;
}
}
用法示例:
PHP;">
where("name = 'zhu'")->limit("5,10");
var_dump($a -> find("name"));
更多关于PHP相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》、《》、《》及《》
希望本文所述对大家PHP程序设计有所帮助。
原文链接:https://www.f2er.com/php/18493.html