一个mysql数据库类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php  
/\*\*
\* 利用魔术方法__call模拟数据库连贯操作
*/
class DB
{

public $sql=array(
'field'=>'',
'where'=>'',
'order'=>'',
'limit'=>''
);


function __call($name,$args){

$name=strtolower($name);
if(array\_key\_exists($name,$this->sql)){
$this->sql\[$name\]=$args\[0\];
}else{
echo 'Class'.get_class().' do not have a method '.$name;
}
return $this;
}

function select(){
echo "SELECT ".$this->sql\['field'\]." FROM user ".$this->sql\['where'\]." ".
$this->sql\['order'\]." ".$this->sql\['limit'\];
}
}

$obj =new DB();
$obj->field("id,name,time")
->where("where id>9")
->order('order by id')
->limit(10)
->select();

?>
0%