博客列表 >接口操作数据表:CURD----2019年6月18日22:30分

接口操作数据表:CURD----2019年6月18日22:30分

白守的博客
白守的博客原创
2019年06月29日 21:47:47652浏览

实例

<?php   

// 这里说明一下,代码是一个个的写的,但是注释是复制的,为了防止我表达错误

// 接口开发数据库链接功能,拥有功能,增删改查
// 定义一个接口
interface iCurd
{
    // 增加数据
    public function create($data);
    // 读取数据
    public function read();
    // 更新数据
    public function update($data ,$where);
    // 删除数据
    public function delete($where);
}

// 创建一个子类 ,实现iCurd接口,完成基本的数据库操作

class Db implements iCurd
{   
    // 数据库的连接对象
    protected $pdo = null;

    // 数据表名
    protected $table;

    // 构造方法:连接数据库,并设置默认数据库名称
    public function __construct($dsn,$user,$password,$table='staff')
    {
        $this->pdo = new PDO($dsn, $user, $password);
        $this->table = $table;
    }

    // 读取
    public function read($fields='*' , $where='',$limit='0,5')
    {
        // 设置查询的条件
        // 判断是否为空
        $where = empty($where) ? '' : 'WHERE ' . $where;
        // 设置显示数量
        $limit = ' LIMIT '.$limit;

        // 预处理查询操作
        $sql = 'SELECT '. $fields.'FROM ' .$this->table.$where.$limit;
        $stmt = $this->pdo->prepare($sql);
        $stmt->execute();
        
        // 返回二维数组表示的查询结果集
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }
    
    // 新增,参数是数组:新记录的键值对
    public function create($data)
    {
        // 字段列表
        $fields = '(name,age,sex,position,mobile,hiredate)';
        // 值列表
        $values = '(:name,:age,:sex,:position,:mobile,:hiredate)';
        // 创建sql语句
        $sql = 'INSERT INTO '. $this->table.$fields. 'VALUES '.$values;
        // 预处理执行新增操作
        $stmt = $this->pdo->prepare($sql);
        $stmt->execute($data);

        // 返回新增数量, 新增记录的ID组成的数组
        return [
            'count'=>$stmt->rowCount(),
            'id'=>$this->pdo->lastInsertId()
        ];
    }



    // 更新,为了数据安全, 不允许无条件更新
    public function update($data,$where)
    {
// 获取数组的键名组成的数组
        $keyarr = array_keys($data);
        $set = '';
        // 遍历键名表示的字段列表,拼装预处理需要的sql语句,注意占符符的表示
        foreach($keyarr as $value){
            $set .=$value . ' = ' .$value. ',';
        }
        // 去掉最后一个逗号, 注意每个逗号后有一个空格,去除时也要带上这个空格
        $set = rtrim($set,', ');
        // 预处理执行更新操作
        $sql = 'UPDATE '. $this->table.' SET '.$set . 'WHERE ' .$where;
        $stmt = $this->pdo->prepare($sql);
        $stmt->execute($data);

 // return返回被更新的记录数量

        return $stmt->rowCount();

    }
// 删除操作,和更新一样,这也是危险的写操作, 不允许无条件删除
    public function delete($where)
    {
        // 预处理执行删除操作(创建一个sql语句)
        $sql = 'DELETE FROM' . $this->table. 'WHERE ' . $where;
        // 准备执行sql语句
        $stmt = $this->pdo->prepare($sql);
        // 执行sql语句
        $stmt->execute();
        // return返回删除语句(不知道是不是应该这样说)
        return $stmt->rowCount();
    }
}

// 客户端的测试代码
// 实例化Db类
$dsn = 'mysql:host=localhost;dbname=php';
$user = 'root';
$password = 'root';
$db = new Db($dsn, $user, $password);

// // echo '克拉无法将刊发我';


// 查询
foreach ($db->read() as $it){
    // var_dump($it);
    echo "<li>名字:{$it['name']}--年龄:{$it['age']}----职位:{$it['position']}</li>";
}
// // 有条件的查询
// echo '<hr>';
// foreach ($db->read('staff_id, name, position', 'age > 50') as $item) {
//     print_r($item); echo '<br>';
// }

$data = [
    'name'=>'郭靖',
    'age'=>30,
    'sex'=>1,
    'position'=>'金刀驸马',
    'mobile'=>'13666668888',
    'hiredate'=>time()
];
$res = $db->create($data);
echo '成功新增'.$res['count'].'条记录,最新记录的主键ID是: '.$res['id'];

// echo '<hr>';

// 更新记录
// 更新刚刚添加的:郭靖

$data = [
    'age' => 40,
    'position'=>'抗金英雄'
];
$where = 'staff_id = 11';
echo '成功更新了: ' .$db->update($data, $where). ' 条记录';


echo '<hr>';

// 删除记录

$where = 'staff_id = 11';
echo '成功更新了: ' .$db->delete($where). ' 条记录';

运行实例 »

点击 "运行实例" 按钮查看在线实例

 

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议