pdo = new PDO('mysql:host='.$config['host'].';dbname='.$config['dbname'].';charset='.$charset.';port='.$port.'',$config['user'],$config['password']);
//开始事务回滚的错误警告
$this->pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
}catch(PDOException $e){
echo 'msyql error: '.$e->getMessage();
}
}
//外部实例化
public static function getInstance($config,$port=3306,$charset='utf8'){
if(!self::$instance instanceof Db){
self::$instance = new self($config,$port,$charset);
}
return self::$instance;
}
//插入数据
public function insertData($table,$data){
//拼接插入的数据库字段名
$columns = implode(',',array_keys($data));
//凭借插入的数据
$values = ':'.implode(',:',array_keys($data));
$sql = "insert into $table ($columns) values ($values)";
//处理date数据格式
$newdata = $this->doData($data);
//预处理sql,执行添加数据操作
$this->doSql($sql,$newdata);
//成功执行后返回插入数据的id,失败返回错误信息
if($this->stmt->errorCode()==00000){
return $this->pdo->lastInsertId();
}else{
return $this->stmt->errorInfo()[2];
}
}
//更新数据
public function updateData($table,$data,$where){
//检查需要更新的数据是否存在
if(!$this->getOne($table,array_keys($data)[0],$where)){
echo '需要更新的数据不存在';
return false;
}
//检查传入的数据并且拼接成字符串
if(is_array($data)){
$columns = '';
foreach($data as $k=>$v){
$columns.= $k.'=:'.$k.',';
}
$columns=rtrim($columns,',');
}
//sql语句
$sql = "UPDATE $table SET $columns WHERE ".$where;
//传入的数组处理成pdo定的数据格式[':user_name'=>'lisi',':user_age'=>39]
$newdata = $this->doData($data);
//预处理sql,执行添加数据操作
$this->doSql($sql,$newdata);
//没有错误返回1,有错误返回错误信息
if($this->stmt->errorCode()==00000){
return 1;
}else{
return $this->stmt->errorInfo()[2];
}
}
//查询一条数据
public function getOne($table,$fields,$where){
//检查传入的字段是否是数组
if(is_array($fields)){
if(count($fields)>1){
$columns=implode(',',$fields);
}else{
$columns=$fields[0];
}
}else{
$columns = $fields;
}
//查询语句
$sql = "SELECT $columns FROM $table WHERE ".$where.' LIMIT 1';
//预处理sql,执行添加数据操作
$this->doSql($sql);
//获得查询的结果集
$res = $this->stmt->fetch(PDO::FETCH_ASSOC);
//如果没有错误则返回查询的结果集,否则返回错误信息
if($this->stmt->errorCode()==00000){
return $res;
}else{
return $this->stmt->errorInfo()[2];
}
}
//查询多条数据
public function getAll($table,$fields,$where=1){
//检查传入的字段是否是数组
if(is_array($fields)){
if(count($fields)>1){
$columns=implode(',',$fields);
}else{
$columns=$fields[0];
}
}else{
$columns = $fields;
}
//查询语句
$sql = "SELECT $columns FROM $table WHERE ".$where;
//预处理sql,执行添加数据操作
$this->doSql($sql);
//获得查询的结果集
$res = $this->stmt->fetchALL(PDO::FETCH_ASSOC);
//如果没有错误则返回查询的结果集,否则返回错误信息
if($this->stmt->errorCode()==00000){
return $res;
}else{
return $this->stmt->errorInfo()[2];
}
}
//删除数据
public function delData($table,$where){
//检查需要更新的数据是否存在
if(!$this->getOne($table,['*'],$where)){
echo '需要删除的数据不存在';
return false;
}
if(!$where){
echo '请添加删除条件';
return false;
}
$sql = "DELETE FROM $table WHERE ".$where;
//预处理sql,执行添加数据操作
$this->doSql($sql);
//如果没有错误则返回查询的结果集,否则返回错误信息
if($this->stmt->errorCode()==00000){
return 1;
}else{
return $this->stmt->errorInfo()[2];
}
}
//处理传入的数据
private function doData($data){
foreach($data as $k=>$v){
$newk = ':'.$k;
$newdata[$newk]=$v;
}
return $newdata;
}
//执行sql操作
function doSql($sql,$data=null){
//预处理sql
$this->stmt=$this->pdo->prepare($sql);
//执行添加数据操作
$this->stmt->execute($data);
}
//禁止外部克隆
private function __clone(){}
}
$config = ['host'=>'127.0.0.1','dbname'=>'shop','user'=>'root','password'=>'root'];
$db = Db::getInstance($config,3306,'utf8');
//插入数据
$table= 'user';
$data=['user_name'=>'sunliang','user_age'=>28,'sex'=>1,'email'=>'[email protected]','password'=>md5(123456),'money'=>544545.34];
echo $db->insertData($table,$data);
// $sql = 'insert into (username,password) values (:username,password)';
// $stmt = $db->prepare($sql);
// $data = [':username'=>'lisi',':password'=>123456];
// $stmt->execute($data);
//修改数据
// $table = 'user';
// $data=['user_name'=>'sunliang','user_age'=>28,'email'=>'[email protected]'];
// $sql = "update user set user_name=:username,user_age=:user_age,email=:email where id = 1";
// echo $db->updateData($table,$data,'id=18');
// $stmt = $db->prepare($sql);
// $stmt->execute($data);
//查询一条数据
// $table = 'user';
// $fields= ['user_name','id'];
// $sql = 'select user_name,email from user where id = 1';
// print_r($db->getOne($table,$fields,'id=2'));
//查询多条数据
// $table = 'user';
// $fields= ['user_name','id'];
// $sql = 'select user_name,email from user where id = 1';
// print_r($db->getAll($table,$fields));
//删除数据
// $table = 'user';
// echo $db->delData($table,'id=16');
简单的数据库类