<BR>/** <BR>* 作者:胡睿 <BR>* 日期:2012/07/21 <BR>* 电邮:[email protected] <BR>*/ <br><br>class HRDB{ <BR>protected $pdo; <BR>protected $res; <BR>protected $config<i style="color:transparent">本¥文来源gaodai$ma#com搞$代*码*网(</i><strong>搞代gaodaima码</strong>; <br><br>/*构造函数*/ <BR>function __construct($config){ <BR>$this->Config = $config; <BR>$this->connect(); <BR>} <br><br>/*数据库连接*/ <BR>public function connect(){ <BR>$this->pdo = new PDO($this->Config['dsn'], $this->Config['name'], $this->Config['password']); <BR>$this->pdo->query('set names utf8;'); <BR>//把结果序列化成stdClass <BR>//$this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); <BR>//自己写代码捕获Exception <BR>$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); <BR>} <br><br>/*数据库关闭*/ <BR>public function close(){ <BR>$this->pdo = null; <BR>} <br><br>public function query($sql){ <BR>$res = $this->pdo->query($sql); <BR>if($res){ <BR>$this->res = $res; <BR>} <BR>} <BR>public function exec($sql){ <BR>$res = $this->pdo->exec($sql); <BR>if($res){ <BR>$this->res = $res; <BR>} <BR>} <BR>public function fetchAll(){ <BR>return $this->res->fetchAll(); <BR>} <BR>public function fetch(){ <BR>return $this->res->fetch(); <BR>} <BR>public function fetchColumn(){ <BR>return $this->res->fetchColumn(); <BR>} <BR>public function lastInsertId(){ <BR>return $this->res->lastInsertId(); <BR>} <br><br>/** <BR>* 参数说明 <BR>* int $debug 是否开启调试,开启则输出sql语句 <BR>* 0 不开启 <BR>* 1 开启 <BR>* 2 开启并终止程序 <BR>* int $mode 返回类型 <BR>* 0 返回多条记录 <BR>* 1 返回单条记录 <BR>* 2 返回行数 <BR>* string/array $table 数据库表,两种传值模式 <BR>* 普通模式: <BR>* 'tb_member, tb_money' <BR>* 数组模式: <BR>* array('tb_member', 'tb_money') <BR>* string/array $fields 需要查询的数据库字段,允许为空,默认为查找全部,两种传值模式 <BR>* 普通模式: <BR>* 'username, password' <BR>* 数组模式: <BR>* array('username', 'password') <BR>* string/array $sqlwhere 查询条件,允许为空,两种传值模式 <BR>* 普通模式: <BR>* 'and type = 1 and username like "%os%"' <BR>* 数组模式: <BR>* array('type = 1', 'username like "%os%"') <BR>* string $orderby 排序,默认为id倒序 <BR>*/ <BR>public function select($debug, $mode, $table, $fields="*", $sqlwhere="", $orderby="tbid desc"){ <BR>//参数处理 <BR>if(is_array($table)){ <BR>$table = implode(', ', $table); <BR>} <BR>if(is_array($fields)){ <BR>$fields = implode(', ', $fields); <BR>} <BR>if(is_array($sqlwhere)){ <BR>$sqlwhere = ' and '.implode(' and ', $sqlwhere); <BR>} <BR>//数据库操作 <BR>if($debug === 0){ <BR>if($mode === 2){ <BR>$this->query("select count(tbid) from $table where 1=1 $sqlwhere"); <BR>$return = $this->fetchColumn(); <BR>}else if($mode === 1){ <BR>$this->query("select $fields from $table where 1=1 $sqlwhere order by $orderby"); <BR>$return = $this->fetch(); <BR>}else{ <BR>$this->query("select $fields from $table where 1=1 $sqlwhere order by $orderby"); <BR>$return = $this->fetchAll(); <BR>} <BR>return $return; <BR>}else{ <BR>if($mode === 2){ <BR>echo "select count(tbid) from $table where 1=1 $sqlwhere"; <BR>}else if($mode === 1){ <BR>echo "select $fields from $table where 1=1 $sqlwhere order by $orderby"; <BR>} <BR>else{ <BR>echo "select $fields from $table where 1=1 $sqlwhere order by $orderby"; <BR>} <BR>if($debug === 2){ <BR>exit; <BR>} <BR>} <BR>} <br><br>/** <BR>* 参数说明 <BR>* int $debug 是否开启调试,开启则输出sql语句 <BR>* 0 不开启 <BR>* 1 开启 <BR>* 2 开启并终止程序 <BR>* int $mode 返回类型 <BR>* 0 无返回信息 <BR>* 1 返回执行条目数 <BR>* 2 返回最后一次插入记录的id <BR>* string/array $table 数据库表,两种传值模式 <BR>* 普通模式: <BR>* 'tb_member, tb_money' <BR>* 数组模式: <BR>* array('tb_member', 'tb_money') <BR>* string/array $set 需要插入的字段及内容,两种传值模式 <BR>* 普通模式: <BR>* 'username = "test", type = 1, dt = now()' <BR>* 数组模式: <BR>* array('username = "test"', 'type = 1', 'dt = now()') <BR>*/ <BR>public function insert($debug, $mode, $table, $set){ <BR>//参数处理 <BR>if(is_array($table)){ <BR>$table = implode(', ', $table); <BR>} <BR>if(is_array($set)){ <BR>$set = implode(', ', $set); <BR>} <BR>//数据库操作 <BR>if($debug === 0){ <BR>if($mode === 2){ <BR>$this->query("insert into $table set $set"); <BR>$return = $this->lastInsertId(); <BR>}else if($mode === 1){ <BR>$this->exec("insert into $table set $set"); <BR>$return = $this->res; <BR>}else{ <BR>$this->query("insert into $table set $set"); <BR>$return = NULL; <BR>} <BR>return $return; <BR>}else{ <BR>echo "insert into $table set $set"; <BR>if($debug === 2){ <BR>exit; <BR>} <BR>} <BR>} <br><br>/** <BR>* 参数说明 <BR>* int $debug 是否开启调试,开启则输出sql语句 <BR>* 0 不开启 <BR>* 1 开启 <BR>* 2 开启并终止程序 <BR>* int $mode 返回类型 <BR>* 0 无返回信息 <BR>* 1 返回执行条目数 <BR>* string $table 数据库表,两种传值模式 <BR>* 普通模式: <BR>* 'tb_member, tb_money' <BR>* 数组模式: <BR>* array('tb_member', 'tb_money') <BR>* string/array $set 需要更新的字段及内容,两种传值模式 <BR>* 普通模式: <BR>* 'username = "test", type = 1, dt = now()' <BR>* 数组模式: <BR>* array('username = "test"', 'type = 1', 'dt = now()') <BR>* string/array $sqlwhere 修改条件,允许为空,两种传值模式 <BR>* 普通模式: <BR>* 'and type = 1 and username like "%os%"' <BR>* 数组模式: <BR>* array('type = 1', 'username like "%os%"') <BR>*/ <BR>public function update($debug, $mode, $table, $set, $sqlwhere=""){ <BR>//参数处理 <BR>if(is_array($table)){ <BR>$table = implode(', ', $table); <BR>} <BR>if(is_array($set)){ <BR>$set = implode(', ', $set); <BR>} <BR>if(is_array($sqlwhere)){ <BR>$sqlwhere = ' and '.implode(' and ', $sqlwhere); <BR>} <BR>//数据库操作 <BR>if($debug === 0){ <BR>if($mode === 1){ <BR>$this->exec("update $table set $set where 1=1 $sqlwhere"); <BR>$return = $this->res; <BR>}else{ <BR>$this->query("update $table set $set where 1=1 $sqlwhere"); <BR>$return = NULL; <BR>} <BR>return $return; <BR>}else{ <BR>echo "update $table set $set where 1=1 $sqlwhere"; <BR>if($debug === 2){ <BR>exit; <BR>} <BR>} <BR>} <br><br>/** <BR>* 参数说明 <BR>* int $debug 是否开启调试,开启则输出sql语句 <BR>* 0 不开启 <BR>* 1 开启 <BR>* 2 开启并终止程序 <BR>* int $mode 返回类型 <BR>* 0 无返回信息 <BR>* 1 返回执行条目数 <BR>* string $table 数据库表 <BR>* string/array $sqlwhere 删除条件,允许为空,两种传值模式 <BR>* 普通模式: <BR>* 'and type = 1 and username like "%os%"' <BR>* 数组模式: <BR>* array('type = 1', 'username like "%os%"') <BR>*/ <BR>public function delete($debug, $mode, $table, $sqlwhere=""){ <BR>//参数处理 <BR>if(is_array($sqlwhere)){ <BR>$sqlwhere = ' and '.implode(' and ', $sqlwhere); <BR>} <BR>//数据库操作 <BR>if($debug === 0){ <BR>if($mode === 1){ <BR>$this->exec("delete from $table where 1=1 $sqlwhere"); <BR>$return = $this->res; <BR>}else{ <BR>$this->query("delete from $table where 1=1 $sqlwhere"); <BR>$return = NULL; <BR>} <BR>return $return; <BR>}else{ <BR>echo "delete from $table where 1=1 $sqlwhere"; <BR>if($debug === 2){ <BR>exit; <BR>} <BR>} <BR>} <BR>} <BR>
其实使用上,和之前的相差不大,目的就是为了方便移植。
本次重写着重处理了几个问题:
① insert语句太复杂,fields与values对应容易出现误差
我们看下最常见的一句sql插入语句
insert into tb_member (username, type, dt) values ('test', 1, now())
在传统模式下,fields和values参数是分开传入的,但却要保证两者参数传入的顺序一致。这很容易导致顺序错乱或者漏传某个参数。
这次已经把问题修改了,采用了mysql独有的insert语法,同样是上面那功能,就可以换成这样的写法
insert into tb_member set username = "test", type = 1, lastlogindt = now()
就像update一样,一目了然。
② 部分参数可以用数组代替
比如这样一句sql
delete from tb_member where 1=1 and tbid = 1 and username = "hooray"
在原先调用方法的时候,需要手动拼装好where条件,这样操作的成本很高,现在完全可以用这种形式
<BR>$where = array( <BR>'tbid = 1', <BR>'username = "hooray"' <BR>); <BR>$db->delete(1, 0, 'tb_member', $where); <BR>
条件再多也不会打乱你的思路。同样,不仅仅是where参数,update里的set也可以以这种形式(具体可参见完整源码)
<BR>$set = array('username = "123"', 'type = 1', 'lastlogindt = now()'); <BR>$where = array('tbid = 1'); <BR>$db->update(1, 0, 'tb_member', $set, $where); <BR>
③ 可自定义sql语句
有时候,sql过于复杂,导致无法使用类里提供的方法去组装sql语句,这时候就需要一个功能,就是能直接传入我已经组装好的sql语句执行,并返回信息。现在,这功能也有了
<BR>$db->query('select username, password from tb_member'); <BR>$rs = $db->fetchAll(); <BR>
是不是很像pdo原生态的写法?
④ 支持创建多数据库连接
原先的因为只是数据库操作方法,所以并不支持多数据库连接,在实现上需要复制出2个相同的文件,修改部分变量,操作实属复杂。现在这问题也解决了。
<BR>$db_hoorayos_config = array( <BR>'dsn'=>'mysql:host=localhost;dbname=hoorayos', <BR>'name'=>'root', <BR>'password'=>'hooray' <BR>); <BR>$db = new HRDB($db_hoorayos_config); <br><br>$db_hoorayos_config2 = array( <BR>'dsn'=>'mysql:host=localhost;dbname=hoorayos2', <BR>'name'=>'root', <BR>'password'=>'hooray' <BR>); <BR>$db2 = new HRDB($db_hoorayos_config2); <BR>
这样就能同时创建2个数据库连接,方便处理数据库与数据库交互的情况。
大致新功能就是这么多了,整个代码并不多,欢迎阅读了解。下面是我在编写时写的测试代码,也一并提供上来,方便大家学习。
<BR>require_once('global.php'); <BR>require_once('inc/setting.inc.php'); <br><br>$db = new HRDB($db_hoorayos_config); <br><br>echo '<hr><b>select测试</b><hr>'; <BR>echo '普通模式,直接字符串传入<br>'; <BR>$rs = $db->select(1, 0, 'tb_member', 'username, password', 'and type = 1 and username like "%os%"'); <BR>echo '<br>数组模式,可传入数组<br>'; <BR>$fields = array('username', 'password'); <BR>$where = array('type = 1', 'username like "%os%"'); <BR>$rs = $db->select(1, 0, 'tb_member', $fields, $where); <br><br>echo '<hr><b>insert测试</b><hr>'; <BR>echo '普通模式,直接字符串传入<br>'; <BR>$db->insert(1, 0, 'tb_member', 'username = "test", type = 1, lastlogindt = now()'); <BR>echo '<br>数组模式,可传入数组<br>'; <BR>$set = array('username = "test"', 'type = 1', 'lastlogindt = now()'); <BR>$db->insert(1, 0, 'tb_member', $set); <br><br>echo '<hr><b>update测试</b><hr>'; <BR>echo '普通模式,直接字符串传入<br>'; <BR>$db->update(1, 0, 'tb_member', 'username = "123", type = 1, lastlogindt = now()', 'and tbid = 7'); <BR>echo '<br>数组模式,可传入数组<br>'; <BR>$set = array('username = "123"', 'type = 1', 'lastlogindt = now()'); <BR>$where = array('tbid = 1'); <BR>$db->update(1, 0, 'tb_member', $set, $where); <br><br>echo '<hr><b>delete测试</b><hr>'; <BR>echo '普通模式,直接字符串传入<br>'; <BR>$db->delete(1, 0, 'tb_member', 'and tbid = 1 and username = "hooray"'); <BR>echo '<br>数组模式,可传入数组<br>'; <BR>$where = array( <BR>'tbid = 1', <BR>'username = "hooray"' <BR>); <BR>$db->delete(1, 0, 'tb_member', $where); <br><br>echo '<hr><b>自定义sql</b><hr>'; <BR>$db->query('select username, password from tb_member'); <BR>$rs = $db->fetchAll(); <BR>var_dump($rs); <br><br>$db->close(); <BR>
作者:胡尐睿丶