单例模式链式查询

来源:互联网 发布:db2 sql 当前系统日期 编辑:程序博客网 时间:2024/06/05 20:08
<?php
class DB{
/*单例模式:解决对象只能实例化一次的问题*/
private static $instance;
public $link;
private function __construct($conf){
$this->link = @mysql_connect($conf['host'],$conf['user'],$conf['pswd']);
mysql_select_db($conf['db']);
mysql_query("set names utf8");
}
private function __clone(){


}
public static function Instance($conf){
if(!(self::$instance instanceof self)){
self::$instance = new self($conf);
}
return self::$instance;
}


public $table;
public function table($table){
$this->table = $table;
return $this;
}


public $where;
public function where($where){
$this->where = " where ".$where;
return $this;
}


public $top;
public function limit($top){
$this->top = " limit ".$top;
return $this;
}


public function query(){
$sql = "select * from ".$this->table.$this->where.$this->top;
$res = mysql_query($sql);
$result = array();
while ($row = mysql_fetch_array($res)) {
$result[] = $row;
}
return $result;
}


}




$conf = array(
"host" => "localhost",
"user" => "root",
"pswd" => "root",
"db" => "hxrz"
);
$obj = DB::Instance($conf);
$res = $obj->table("test")->where("id > 0 ")->limit(2)->query();
print_r($res);
?>