Correcting teacher:查无此人
Correction status:qualified
Teacher's comments:完成的不错,继续加油。
class DB{
public $dsn;
public $user;
public $password;
public $pdo;
public $table;
// public $table;
// public $fields;
// public $where;
// public $order;
// public $limit;
// public $stmt;
public function __construct($dsn,$user,$password,$table)
{
$this->dsn = $dsn;
$this->user = $user;
$this->password = $password;
$this->table = $table;
$this->connect();
//已经预处理,后面函数不用添加此语句了
}
public function connect(){
$this->pdo = new PDO($this->dsn,$this->user,$this->password);
}
public function select($fields, $where='', $order='',$limit=''){
// $this->table = $table;
$sql = 'SELECT ';
if (is_array($fields)) {
foreach ($fields as $field) {
$sql .= $field.', ';
}
} else {
$sql .= $fields;
}
$sql = rtrim(trim($sql),',');
$sql .= ' FROM '.$this->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($fields,$where=''){
$sql = 'SELECT ';
if (is_array($fields)) {
foreach ($fields as $field) {
$sql .= $field.', ';
}
} else {
$sql .= $fields;
}
$sql = rtrim(trim($sql),',');
$sql .= ' FROM '.$this->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($data=[]){
$sql = "INSERT INTO {$this->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 true;
}
} else {
return false;
}
}
public function update($data=[], $where=''){
$sql = "UPDATE {$this->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 true;
}
} else {
return false;
}
}
public function delete($where=''){
$sql = "DELETE FROM {$table} ";
//查询条件
if(!empty($where)){
$sql .= ' WHERE '.$where;
}
//创建PDO预处理对象
$stmt = $this->pdo->prepare($sql);
//执行删除操作
if($stmt->execute()){
if($stmt->rowCount()>0){
return true;
}
} else {
return false;
}
}
public function count_num($where){
$sql = 'SELECT count(*) as count_number FROM '.$this->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;
}
}
}
$db = new DB('mysql:host=localhost;dbname=anguoguo','root','root','movies');
//连接pdo
print_r($db);
$result1 = $db->select('name','cate_id=2','mov_id DESC');
print_r($result1);
//echo是输出类型--Array,结果集用print_r输出
echo '<hr/>';
$result2 = $db->find('image,name','mov_id=5');
print_r($result2);
echo '<hr/>';
$array3 =[
'mov_id' =>10,
'name' =>'插入',
'image'=>'10.jpg',
'detail'=>'能否插入成功',
'cate_id'=>'4'
];
$result3 = $db->insert($array3);
//插入是否有问题--当为数组时,不用加‘’,返回值插入成功--1
print_r($result3);
echo '<hr/>';
$result4 = $db->update('[image=>10.jpg]','mov_id=5');
print_r($result4);
echo '<hr/>';
$result5 = $db->delete('');
print_r($result5);
echo '<hr/>';
$result6 = $db->count_num('mov_id');
print_r($result6);
echo '<hr/>';
类名不区分大小写,函数名区分
构造方法是预处理,不是一定输出