Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:完成的很好
<?php
namespace _1215;
use PDO;
// 数据库查询类
class Db
{
// 连接对象
public $pdo = null;
// 数据表名
public $table = '';
// 字段列表
public $field = '';
// 查询条件
public $where = '';
// 显示数量
public $limit = 0;
// 构造方法,初始化连接对象
public function __construct($pdo)
{
// 连接对象是对象方法的共享属性
$this->pdo = $pdo;
}
// 调用表名
public function table($tableName)
{
$this->table = $tableName;
// 返回当前对象,便于链式调用该对象的其它方法
return $this;
}
// 设置查询字段
public function field($fields=[])
{
$this->field = $fields;
return $this;
}
// 设置查询条件
public function where($where)
{
$this->where = $where;
return $this;
}
// 设置显示数量
public function limit($limit)
{
$this->limit = $limit;
return $this;
}
// 创建SQL查询语句对象,并返回查询结果
public function select()
{
// 查询条件分开设置, 可以确保链式方法独立
$fields = empty($this->field) ? '*' : $this->field;
$where = empty($this->where) ? '' : ' WHERE '.$this->where;
$limit = empty($this->limit) ? '' : ' LIMIT '.$this->limit;
// 接装SQL语句
$sql = 'SELECT '.$fields.' FROM '.$this->table. $where . $limit;
// 预处理查询
$stmt = $this->pdo->prepare($sql);
$stmt->execute();
//return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function insert($data=[])
{
// 查询条件分开设置, 可以确保链式方法独立 INSERT INTO `category` SET `name`='rh', `alias`='欧美猛片';
$fields = empty($this->field) ? '*' : $this->field;
$where = empty($this->where) ? '' : ' WHERE '.$this->where;
$limit = empty($this->limit) ? '' : ' LIMIT '.$this->limit;
// 接装SQL语句
//$sql = 'INSERT INTO '.$this->table.' SET '.'('.'`'.$fields[0].'`'.','.'`'.$fields[1].'`'.') VALUES '.'('.'`'.$data[0].'`'.','.'`'.$data[1].'`'.');
$sql = 'INSERT INTO '.$this->table.' SET '.'`'.$fields[0].'`'.' = '.'\''.$data[0].'\''.','.'`'.$fields[1].'`'.' = '.'\''.$data[1].'\'';
var_dump($sql);
// 预处理查询
$stmt = $this->pdo->prepare($sql);
$stmt->execute();
if ($stmt->rowCount()>0){
echo '插入数据 '.$stmt->rowCount().'条';
}else{
echo '插入数据失败';
}
}
public function update($data)
{
// 查询条件分开设置, 可以确保链式方法独立 UPDATE category SET `alias`='日韩新片' WHERE cate_id = 3
$fields = empty($this->field) ? '*' : $this->field;
$where = empty($this->where) ? '' : ' WHERE '.$this->where;
$limit = empty($this->limit) ? '' : ' LIMIT '.$this->limit;
// 接装SQL语句
$sql = 'UPDATE '.$this->table. ' SET '.'`'.$fields .'`'.' = '.'\''.$data.'\''.$where;
var_dump($sql);
// 预处理查询
$stmt = $this->pdo->prepare($sql);
$stmt->execute();
//return $stmt->fetchAll(PDO::FETCH_ASSOC);
if ($stmt->rowCount()>0){
echo '更新数据成功 '.$stmt->rowCount().'条';
}else{
echo '更新数据失败';
}
}
public function delete()
{
// 查询条件分开设置, 可以确保链式方法独立 DELETE FROM movies WHERE mov_id = 9
$fields = empty($this->field) ? '*' : $this->field;
$where = empty($this->where) ? '' : ' WHERE '.$this->where;
$limit = empty($this->limit) ? '' : ' LIMIT '.$this->limit;
// 接装SQL语句
$sql = 'DELETE FROM '.$this->table.$where;
var_dump($sql);
// 预处理查询
$stmt = $this->pdo->prepare($sql);
$stmt->execute();
//return $stmt->fetchAll(PDO::FETCH_ASSOC);
if ($stmt->rowCount()>0){
echo '删除数据成功 '.$stmt->rowCount().'条';
}else{
echo '删除数据失败';
}
}
}
$db = new Db(new PDO('mysql:host=127.0.0.1;dbname=mt','root','111'));
$result = $db->table('user')
->field([name,age])
->insert([hhappysong,50]);
var_dump($result)
$result = $db->table('user')
->field('name')
->where('uid = 12')
->update('任双海');
var_dump($result);
$res = $db->table('user')
->where('uid>2')
->delete();
var_dump($res);
$res = $db->table('user')
->where('uid=9')
->delete();
var_dump($res)
新增记录SQL语句中,需要用到键值对形式,因此设想把字段名$fields和需要新增数据$data设置成数组$fields=[],$data=[],然后按照两个数组一一对应,这时能够生成正确的SQL语句,也能正常插入新数据,但是报错;
public function insert($data=[])
{
// 查询条件分开设置, 可以确保链式方法独立 INSERT INTO `category` SET `name`='rh', `alias`='欧美猛片';
$fields = empty($this->field) ? '*' : $this->field;
$where = empty($this->where) ? '' : ' WHERE '.$this->where;
$limit = empty($this->limit) ? '' : ' LIMIT '.$this->limit;
// 接装SQL语句
//$sql = 'INSERT INTO '.$this->table.' SET '.'('.'`'.$fields[0].'`'.','.'`'.$fields[1].'`'.') VALUES '.'('.'`'.$data[0].'`'.','.'`'.$data[1].'`'.');
$sql = 'INSERT INTO '.$this->table.' SET '.'`'.$fields[0].'`'.' = '.'\''.$data[0].'\''.','.'`'.$fields[1].'`'.' = '.'\''.$data[1].'\'';
var_dump($sql);
// 预处理查询
$stmt = $this->pdo->prepare($sql);
$stmt->execute();
if ($stmt->rowCount()>0){
echo '插入数据 '.$stmt->rowCount().'条';
}else{
echo '插入数据失败';
}
}
提示错误如下: