为了方便“按自己的习惯”操作mysql,做的一个继承PDO的简单class。
<?php
class Db extends PDO {
protected static $_instance = null;
public static function instance(){
if(self::$_instance == null){
$hostname = $GLOBALS['hostname'];
$db_config = $GLOBALS['_CONFIG']["{$hostname}"]['db'];
self::$_instance = self::int_db($db_config);
self::$_instance->exec('set names utf8');
}
return self::$_instance;
}
public static function int_db($db_config){
$dsn = 'mysql:host='.$db_config['host'].';dbname='.$db_config['dbname'];
$user = $db_config['user'];
$pass = $db_config['passwd'];
try{
$dbh = new self($dsn, $user,$pass,array(PDO::ATTR_PERSISTENT => true));
return $dbh;
}catch(PDOException $e){
echo 'db error';
return false;
}
}
public function fetchAll($sql, array $params = null){
$stmt = $this->prepare($sql);
$stmt->execute($params);
return $stmt->fetchAll();
}
public function fetchRow($sql, array $params = null){
$list = $this->fetchAll($sql,$params);
if(isset($list[0])){
return $list[0];
}
else{
return array();
}
}
public function fetchOne($sql, array $params = null){
$row = $this->fetchRow($sql,$params);
if(isset($row[0])){
return $row[0];
}
else{
return null;
}
}
public function update($table_name, array $bind, array $where = null){
if(empty($bind)){
return false;
}
$sql = 'update '.$table_name.' set ';
$set = array();
foreach($bind as $key=>$value){
$set[] = '`'.$key.'`' . ' = :' . $key;
}
$sql .= implode(',',$set);
if(!empty($where)){
foreach($where as $k=>$v){
if(!is_numeric($k)){
die('update where key is not number');
}
}
$sql = $sql . ' where ' . implode(' and ',$where);
}
$stmt = $this->prepare($sql);
return $stmt->execute($bind);
}
public function insert($table_name, array $bind){
if(empty($bind)){
return false;
}
$sql = 'insert into '.$table_name.'';
$fileds = array_keys($bind);
$sql = $sql . ' (`'.implode('`,`',$fileds) . '`) values (:'. implode(", :",$fileds) .')';
$stmt = $this->prepare($sql);
return $stmt->execute($bind);
}
public function delete($table_name, array $where = null){
$sql = 'delete from '.$table_name.'';
if(!empty($where)){
foreach($where as $k=>$v){
if(!is_numeric($k)){
die('delete where key is not number');
}
}
$sql = $sql . ' where ' . implode(' and ',$where);
}
$stmt = $this->prepare($sql);
return $stmt->execute();
}
public function replace($table_name, array $bind){
if(empty($bind)){
return false;
}
$sql = 'replace into '.$table_name.'';
$fileds = array_keys($bind);
$sql = $sql . ' (`'.implode('`,`',$fileds) . '`) values (:'. implode(", :",$fileds) .')';
$stmt = $this->prepare($sql);
return $stmt->execute($bind);
}
}
?>
//设置主机名
$hostname = "server_development";
//db连接配置
$_CONFIG['server_production']['db'] = array(
'host' => '127.0.0.1',
'dbname' => 'production',
'user' => 'username',
'passwd' => 'password',
);
$_CONFIG['server_development']['db'] = array(
'host' => '127.0.0.1',
'dbname' => 'development',
'user' => 'username',
'passwd' => 'password',
);
//举例
//update:
Db::instance()->update('table1', array('isDeleted'=>1), array("status=4","gender=2"));
//相当于sql "update table1 set isDeleted = 1 where status = 4 and gender = 2"
//replace
$bind = array();
$bind['name'] = 'john';
$bind['dateline'] = time();
Db::instance()->replace('table1', $bind);
//相当于sql "replace into table1 (name,dateline) values ('john',".time().")"
//insert
$db = Db::instance();
$bind = array();
$bind['name'] = 'john';
$bind['dateline'] = time();
$db->insert('table1', $bind);
//相当于sql "insert into table1 (name,dateline) values ('john',".time().")"
$lastInsertId = $db->lastInsertId();
//delete
Db::instance()->delete('table1', array("status=4","gender=2"));
//相当于 sql "delete from table1 where status = 4 and gender = 2"
//fetchRow
$row = Db::instance()->fetchRow("select * from table1 where id=1 limit 1");
print_r($row);
//fetchAll
$list = Db::instance()->fetchAll("select * from table1 where gender = 2");
print_r($list);
//fetchOne
$status = Db::instance()->fetchAll("select status from table1 where id = 1");
echo $status;
//delete
Db::instance()->delete('table1', array("status=4","gender=2"));
//相当于 sql "delete from table1 where status = 4 and gender = 2"