单例模式链式查询
来源:互联网 发布: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);
?>
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);
?>
阅读全文
0 0
- 单例模式链式查询
- js单链式运算
- 机房重构——模板方法模式+单例模式+存储过程优化组合查询
- 单例、单例模式
- JDBC实现增删改查、模糊查询、分页查询、子查询以及体现单例设计模式连接数据库
- 单例模式-多线程单例模式
- 单件模式(单例模式)
- 数据结构-单线性链式-头插法
- 设计模式------单例模式
- 设计模式------单例模式
- 设计模式-单例模式
- 设计模式 - 单例模式
- 设计模式---单例模式
- 设计模式---单例模式
- PHP模式-单例模式
- 【设计模式】单例模式
- 设计模式-单例模式
- 设计模式----单例模式
- Android_滑动的时候头部变化效果
- G
- ping++支付
- CodeForces
- SpringMVC + Mybatis 配置多数据源
- 单例模式链式查询
- python
- Android HTTPS、TLS版本支持相关解决方案
- c++动态分配内存 动态申请数组
- 机器学习文章资料汇总
- 推荐系统-协同过滤简单介绍
- XYNU OJ 1000—1009基础题(适合于C语言初学者)
- Android Studio 使用JNI库
- JavaEE 三层结构