class Db{
protected $prefix = '';
protected $pdo = null;
protected $data = [];
protected $sql = [];
protected $error_msg = '';
public function __construct($config){
try{
$dsn = $config['dsn'];
$username = $config['username'];
$password = $config['password'];
$this->pdo = new PDO($dsn, $username, $password); //初始化一个PDO对象
//设置异常处理
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if(!empty($config['prefix'])){
$this->prefix = $config['prefix'];
}
if(!empty($config['charset'])){
$tmp_sql = 'set names '.$config['charset'];
$this->pdo->exec($tmp_sql);
}
}catch(\PDOException $e){
$this->error = $e->getMessage();
}
}
protected function buildCondition($condition){
if(is_numeric($condition)){
$where = 'id='.$condition;
}else if(is_array($condition)){
$where = [];
$reg = '/^([a-z_]+?)\[(.+?)\]$/';
$logic_sign = ['>','=','','in'];
foreach($condition as $key=>$value){
preg_match($reg,$key,$match_result);
if(!empty($match_result) && in_array($match_result[2],$logic_sign)){
if(is_string($value)){
$where[] = $match_result[1].' '.$match_result[2]." '".$value."'";
}else{
$where[] = $match_result[1].' '.$match_result[2].' '.$value;
}
}else{
var_dump(!empty($match_result));
var_dump(in_array($match_result[2],$logic_sign));
die('where条件解析错误');
}
}
$where = implode(' and ',$where);
}else if(is_string($condition)){
$where = $condition;
}else{
die('参数错误');
}
return ' where '.$where;
}
//构建SQL语句
protected function buildSQL($table_name,$data,$stype){
$this->data = [];
switch($stype){
case 'select':
break;
case 'find':
$where = $this->buildCondition($data);
$fields = is_array($this->fields) ? implode(',',$this->fields) : $this->fields;
$sql = 'select '.$fields.' from '.$this->prefix.$table_name.$where.' limit 1';
break;
case 'insert':
$fields = [];
$values = [];
$fields_data = [];
foreach($data as $field=>$value){
$fields[] = '`'.$field.'`';
$values[] = ':'.$field;
$fields_data[':'.$field] = $value;
}
$this->data = $fields_data;
$sql = 'insert into '.$this->prefix.$table_name.'('.implode(',',$fields).')values('.implode(',',$values).');';
break;
case 'update':
case 'delete':
break;
default:
die('错误的参数--stype');
break;
}
return $sql;
}
public function select($table_name='',$fields='*',$condition=[]){
$this->fields = $fields;
//$this->condition = $condition;
if(is_array($fields) == '*'){
$fields = '`'.implode('`,`',$this->fields).'`';
}
$where = $this->buildCondition($condition);
$sql = 'select '.$fields.' from '.$this->prefix.$table_name.$where;
$this->sql[] = $sql;
//prepare:备要执行的SQL语句并返回一个 PDOStatement 对象,,可以理解为结果集
if(empty($this->data)){
$sth = $this->pdo->query($sql);
}else{
$sth = $this->pdo->prepare($sql);
$sth->execute($this->data);
}
$rows_data = $sth->fetchAll(PDO::FETCH_ASSOC);
return $rows_data;
}
public function find($table_name='',$fields,$condition=[]){
$this->table_name = $table_name;
$this->fields = $fields;
//$this->condition = $condition;
$sql = $this->buildSQL($table_name,$condition,'find');
$this->sql[] = $sql;
//prepare:备要执行的SQL语句并返回一个 PDOStatement 对象,,可以理解为结果集
$sth = $this->pdo->prepare($sql);
if(empty($this->data)){
$sth->execute();
}else{
$sth->execute($this->data);
}
//只返回关联数组
$row_data = $sth->fetch(PDO::FETCH_ASSOC);
return $row_data;
}
public function insert($table_name='',$data=[]){
try{
$fields = $values = $fields_data = [];
foreach($data as $field=>$value){
$fields[] = '`'.$field.'`';
$values[] = ':'.$field;
$fields_data[':'.$field] = $value;
}
//$this->data = $fields_data;
$sql = 'insert into '.$this->prefix.$table_name.'('.implode(',',$fields).')values('.implode(',',$values).');';
//$sql = $this->buildSQL($table_name,$data,'insert');
$this->sql[] = $sql;
//echo $sql;die;
//prepare:备要执行的SQL语句并返回一个 PDOStatement 对象,,可以理解为结果集
$sth = $this->pdo->prepare($sql);
$sth->execute($fields_data);
$row_id = (int)$this->pdo->lastInsertId();
return $row_id;
}catch(\PDOException $e){
$this->error_msg = $e->getMessage();
return false;
}
}
public function update($table_name='',$columns=[],$condition=[]){
try{
$data = $sql = [];
foreach($columns as $field=>$value){
$sql[] = is_numeric($value) ? "`".$field."`=:".$field : "`".$field."`=:".$field ;
$data[':'.$field] = $value;
}
$where = $this->buildCondition($condition);
$sql = 'update '.$this->prefix.$table_name.' set '.implode(',',$sql).$where.';';
$this->sql[] = $sql;
$data = array_merge($this->data,$data);
//prepare:备要执行的SQL语句并返回一个 PDOStatement 对象,,可以理解为结果集
$sth = $this->pdo->prepare($sql);
$sth->execute($data);
//rowcount返回受影响的行数
$rows = (int)$sth->rowCount();
return $rows;
}catch(\PDOException $e){
$this->error_msg = $e->getMessage();
return false;
}
}
public function delete($table_name,$condition){
try{
$where = $this->buildCondition($condition);
$sql = 'delete from '.$this->prefix.$table_name.$where.';';
$this->sql[] = $sql;
//prepare:备要执行的SQL语句并返回一个 PDOStatement 对象,,可以理解为结果集
$sth = $this->pdo->prepare($sql);
$sth->execute($this->data);
//rowcount返回受影响的行数
$rows = (int)$sth->rowCount();
return $rows;
}catch(\PDOException $e){
$this->error_msg = $e->getMessage();
return false;
}
}
public function rows($table_name,$condition){
try{
$where = $this->buildCondition($condition);
$sql = 'select count(*) as rows from '.$this->prefix.$table_name.$where.';';
$this->sql[] = $sql;
//prepare:备要执行的SQL语句并返回一个 PDOStatement 对象,,可以理解为结果集
$sth = $this->pdo->prepare($sql);
$sth->execute($this->data);
//rowcount返回受影响的行数
$row_data = $sth->fetch(PDO::FETCH_ASSOC);
return (int)$row_data['rows'];
}catch(\PDOException $e){
$this->error_msg = $e->getMessage();
return false;
}
}
public function startTrans(){
$this->pdo->beginTransaction();
}
public function commit(){
$this->pdo->commit();
}
public function rollback(){
$this->rollback();
}
public function error(){
echo '
';
var_dump($this->sql);
echo '
';
echo $this->error_msg;
die;
//
return $this->error_msg;
}
public function logs(){
return $this->sql;
}
}
一键复制
编辑
Web IDE
原始数据
按行查看
历史