登录  /  注册
博主信息
博文 40
粉丝 0
评论 0
访问量 36547
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
PHP学习总结(14)数据库接口类增、删、改、查——2019年10月9号20:00分
虎子爸爸
原创
641人浏览过

class_d.png

上代码:

实例

<?php
namespace User;

// 先定义一个接口
interface sqlAction{
    //定义4个你必须要实现的方法
    public function create($data);
    public function del($where);
    public function update($data,$where);
    public function read($where);
}
//然后我们新建个操作类继承它
class DB implements sqlAction{
    //老办法,先定义几个属性
    protected $pdo = null;
    protected $table;
    // 然后我们用构造方法直连
    public function __construct($table){
        //记住这里,简单的来吧
        // 先建立起数据库连接,我们这里默认了数据库phpshouce
        $this->pdo = new \PDO('mysql:host=localhost;dbname=phpshouce', 'root', 'root');
        $this->table = $table ;
    }
    // 然后我们要实现接口的4个方法
    // 第一个create
    public function create($data){
        // 字段列表
        $fields = ' (username, password,nickname,status) ';
        // 值列表
        $values = ' (:username, :password,:nickname,:status) ';

        // 创建SQL
        $sql = 'INSERT INTO ' .$this->table .$fields . ' VALUES ' . $values;

        $stmt = $this->pdo->prepare($sql);
        $stmt->execute($data);

        return [
            'count' => $stmt->rowCount(),
            'id' => $this->pdo->lastInsertId()
        ];

    }
    //第二个del
    public function del($where){
        // 写语句

        $sql = 'DELETE FROM ' .$this->table . ' WHERE ' .$where;
        // 预执行
        $res = $this->pdo->prepare($sql);
        // 执行
        $res->execute();
        // 返回
        return $res->rowCount();

    }
    //第三个update
    public function update($data, $where)
    {
        $keyArr = array_keys($data);
        $set = '';
        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 $stmt->rowCount();


    }
    //第四个read
    public function read($fields='*', $where='', $limit='0, 5')
    {
        //设置条件
        $where = empty($where) ? '' : ' WHERE ' . $where;

        // 设置显示数量
        $limit = ' LIMIT ' . $limit;
        $sql_base_a = "select * from fubao_user where id='4' limit '0,5' ";
        $sql_base_b = "select id,username,nickname,status from fubao_user where title like 'z%' limit '0,5' ";

        $sql = 'SELECT ' . $fields . ' FROM ' . $this->table. $where . $limit;
        $stmt = $this->pdo->prepare($sql);
        $stmt->execute();
        return $stmt->fetchAll(\PDO::FETCH_ASSOC);
    }

}



//先实例化
$db = new DB('fubao_user');
// 查询
$fields_read = 'id,username,nickname,status';

$result_read = $db->read('id,username,nickname,status','',5);
// 新增
$data_create = [    
   'username' => 'xiaomao',
   'password' => '123456',   
   'nickname' => '金刀驸马',   
   'status' => '1'
];
$result_create = $db->create($data_create);
// 更新
$data_update = [
    'password'=>'2daada2',
    'status'=>2
];
$where_update = 'id=40';
$result_update = $db->update($data_update,$where_update);
// 删除
$where_del = 'id = 44';
$result_del =  $db->del($where_del);

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<style>
    /*给表格加上边框*/
    
    table {
        border: 1px solid #444444;
        border-collapse: collapse;
        width: 800px;
        margin: 20px auto;
    }
    
    th,
    td {
        border: 1px solid #444444;
        text-align: center;
        padding: 10px;
    }
    
    table caption {
        font-size: 1.3rem;
        /*文本加粗*/
        font-weight: bolder;
        margin-bottom: 15px;
    }
    /* 第一行 */
    
    #table-head-tr {
        background-color: lightgreen;
    }
    /* 第一行 */
    
    table thead>tr:first-of-type {
        /* background-color: lightgreen; */
        color: cornflowerblue;
    }
    
    table tbody>tr:first-of-type>td:first-of-type {
        background-color: wheat;
    }
    
    table tbody>tr:nth-last-of-type(1)>td:first-of-type {
        background-color: crimson;
        color: darkorange;
    }
    /*圆角表格样式*/
    /* 第一行左 */
    
    table tr:first-child th:first-child {
        border-top-left-radius: 12px;
    }
    /* 第一行右 */
    
    table tr:first-child th:last-child {
        border-top-right-radius: 12px;
    }
    
</style>
<body>

<table>
    <caption>数据库输出select</caption>
    <thead>
        <tr>
            <th>ID</th>
            <th>username</th>
            <th>nickname</th>
            <th>status</th>
        </tr>
    </thead>
    <tbody>
        <?php
        foreach( $result_read as $data){
        ?>
            <tr>

            <td><?php echo $data['id']; ?></td>
            <td><?php echo $data['username']; ?></td>
            <td><?php echo $data['nickname']; ?></td>
            <td><?php echo $data['status']; ?></td>
            </tr>

        <?php } ?>
</table> 
<hr>
<h3>  <?php  echo '成功的新增了: '. $result_create['count'].' 条记录,新增的记录的主键ID是: ' . $result_create['id'];  ?>  </h3>
<hr>
<h3>  <?php  echo '成功的更新了: ' . $result_update. ' 条记录';  ?>  </h3>
<hr>
<h3>  <?php  echo '成功的删除了: ' .  $result_del . ' 条记录';  ?>  </h3>

</body>
</html>

运行实例 »

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


批改状态:合格

老师批语:当你把这个作业认真写完, 对于接口编程, 应该会有一个全新的认识
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学