Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:下次记得把总结写上, 便于老师掌握你的情部
<?php
class Db{
// 连接参数
public $dsn;
public $user;
public $password;
// 连接属性
public $pdo;
public function __construct($dsn,$user,$password){
$this->dsn = $dsn;
$this->user = $user;
$this->password = $password;
$this->connect();
}
// 连接方法
public function connect(){
try{
$this->pdo = new PDO($this->dsn,$this->user,$this->password);
}catch (PDOException $e){
die('数据库连接失败,错误信息:' . $e->getMessage());
}
}
// 查询多条记录
function select($table,$fields='*',$where='',$order='',$limit=''){
// 创建SQL语句
$sql = 'SELECT ';
if (is_array($fields)) {
foreach ($fields as $field){
$sql .= $field . ', ';
}
}else{
$sql .= $fields;
}
$sql = rtrim(trim($sql), ',');
$sql .= ' FROM ' . $table;
// 查询条件
if (!empty($where)){
$sql .= ' WHERE ' . $where;
}
// 排序方式
if (!empty($order)){
$sql .= ' ORDER BY ' . $order;
}
// 分页
if (!empty($limit)){
$sql .= ' LIMIT ' . $limit;
}
$sql .= ';';
// 创建PDO预处理对象
$stmt = $this->pdo->prepare($sql);
// 执行查询操作
if($stmt->execute()){
if($stmt->rowCount()>0){
$stmt -> setFetchMode(PDO::FETCH_ASSOC);
return $stmt->fetchAll();
}
}
return '查询失败';
}
// 查询单条记录
function find($table,$fields,$where=''){
$sql = 'SELECT ';
if(is_array($fields)){
foreach ($fields as $field){
$sql .= $field . ', ';
}
}else{
$sql .= $fields;
}
$sql = rtrim(trim($sql),',');
$sql .= ' FROM ' . $table;
if(!empty($where)){
$sql .= ' WHERE ' . $where;
}
$sql .= ' LIMIT 1;';
$stmt = $this->pdo->prepare($sql);
if($stmt->execute()){
if ($stmt->rowCount()>0){
$stmt->setFetchMode(PDO::FETCH_ASSOC);
return $stmt->fetch();
}
}
return '查询失败';
}
// 新增记录
function insert($table,$data=[]){
$sql = 'INSERT INTO ' . $table . ' SET ';
if (is_array($data)){
foreach ($data as $k => $v){
$sql .= "{$k}='{$v}', ";
}
}else{
return '要以关联数组形式传入数据';
}
$sql = rtrim(trim($sql),',') . ';';
$stmt = $this->pdo->prepare($sql);
if ($stmt->execute()){
if($stmt->rowCount()>0){
return '添加成功了' . $stmt->rowCount() . '条数据';
}
}
return '添加失败';
}
// 更新数据
function update($table,$data=[],$where=''){
$sql = "UPDATE {$table} SET ";
if (is_array($data)){
foreach ($data as $k=>$v){
$sql .= "{$k}='{$v}', ";
}
}
$sql = rtrim(trim($sql),',');
if(!empty($where)){
$sql .= ' WHERE ' . $where;
}
$stmt = $this->pdo->prepare($sql);
if ($stmt->execute()){
if($stmt->rowCount()>0){
return '成功修改了' . $stmt->rowCount() . '条数据';
}
}
return '修改失败';
}
// 删除数据
function delete($table,$where=''){
$sql = 'DELETE FROM ' . $table;
if(!empty($where)){
$sql .= ' WHERE ' . $where;
}
$stmt = $this->pdo->prepare($sql);
if($stmt->execute()){
if($stmt->rowCount()>0){
return '删除成功了' . $stmt->rowCount() . '条数据';
}
}
return '删除失败';
}
// 统计数量
function count_num($table,$where=''){
$sql = 'SELECT COUNT(*) AS count_num FROM ' . $table;
if (!empty($where)){
$sql .= ' WHERE ' . $where;
}
$stmt = $this->pdo->prepare($sql);
if($stmt->execute()){
if($stmt->rowCount()>0){
return $stmt->fetch(PDO::FETCH_ASSOC)['count_num'];
}
}
return '输入条件不正确';
}
public function __destruct(){
$this->pdo = null;
}
}