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(){ $this->pdo = new PDO($this->dsn,$this->user,$this->password); } //查询方法 public function select($table,$fields, $where='', $order='',$limit=''){ $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(); } } else { return false; } } //单条数据 public function find($table,$fields,$where=''){ //创建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; } $sql .= ' LIMIT 1;'; //创建PDO预处理对象 $stmt = $this->pdo->prepare($sql); //执行查询操作 if($stmt->execute()){ if($stmt->rowCount()>0){ $stmt->setFetchMode(PDO::FETCH_ASSOC); return $stmt->fetch(); } } else { return false; } } //新增数据 public function insert($table,$data=[]){ //创建SQL语句 $sql = "INSERT INTO {$table} SET "; //组装插入语句 if(is_array($data)){ foreach ($data as $k=>$v) { $sql .= $k.'="'.$v.'", '; } }else{ return false; } //去掉尾部逗号,并添加分号结束 $sql = rtrim(trim($sql),',').';'; //创建PDO预处理对象 $stmt = $this->pdo->prepare($sql); //执行新增操作 if($stmt->execute()){ if($stmt->rowCount()>0){ return 'success'.$stmt->rowCount().'count'; } } else { return false; } } //更新数据 public function update($table,$data=[], $where=''){ //创建SQL语句 $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; } //创建PDO预处理对象 $stmt = $this->pdo->prepare($sql); //执行新增操作 if($stmt->execute()){ if($stmt->rowCount()>0){ return 'update success '.$stmt->rowCount().' count'; } } else { return false; } } //删除数据 public function delete($table,$where=''){ //创建SQL语句 $sql = "DELETE FROM {$table} "; //查询条件 if(!empty($where)){ $sql .= ' WHERE '.$where; } //创建PDO预处理对象 $stmt = $this->pdo->prepare($sql); //执行删除操作 if($stmt->execute()){ if($stmt->rowCount()>0){ return 'delete success '.$stmt->rowCount().' count'; } } else { return false; } } //统计数量 public function count_num($table,$where=''){ $sql = 'SELECT count(*) as count_number FROM '.$table; //查询条件 if(!empty($where)){ $sql .= ' WHERE '.$where; } //创建PDO预处理对象 $stmt = $this->pdo->prepare($sql); //执行查询操作 if($stmt->execute()){ if($stmt->rowCount()>0){ $row = $stmt->fetch(PDO::FETCH_ASSOC); $rows = $row['count_number']; return $rows; } } else { return false; } } public function __destruct() { $this->pdo = null; } }
$db = new Db('mysql:host=localhost;dbname=shy','root','root');
echo '<pre>'.print_r($db->select('shoes','pinpai,color','','color DESC'),true);
echo '<pre>'.print_r($db->find('shoes','number,pinpai','id=5'),true);
echo $db->insert('people',['name'=>'she','age'=>18]);
echo $db->update('people',['name'=>'小宇'],'id=1');
echo $db->delete('people','id=4');
echo $db->count_num('shoes');
手写:
将数据库函数重写到类中,数据操作方便,提高执行效率
ps:最近家里出了点事一直没时间写作业,一定补齐