博主信息
博文 40
粉丝 0
评论 0
访问量 45687
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
PHP学习总结(12)——数据库链接查询原理实例2019年10月9号20:00分
虎子爸爸
原创
975人浏览过

class_c.png

先上Query.php类文件---这里需要注意命名空间

实例

<?php
namespace Article;

// 数据库查询类
class Query
{
    // 先定义一些必须的属性——连接对象
    public $pdo = null;

    // 表名
    public $table;

    // 字段
    public $field = '*';

    // 条件
    public $where;

    // 数量
    public $limit;

    // 构造方法——这里必须
    public function __construct($pdo){
        // 实例时自动连接数据库
        $this->pdo = $pdo;
    }

    // 设置数据表名称
    public function table($tableName) {
        $this->table = $tableName;
        //返回当前类实例, 用来链式调用后面的其它方法
        return $this;
    }

    // 设置数据表字段
    public function field($fields = '*'){
        $this->field = empty($fields) ? '*' : $fields;
        return $this;
    }

    // 设置查询条件
    public function where($where = ''){
        $this->where = empty($where) ? $where : ' WHERE '. $where;
        return $this;
    }

    // 设置显示数量
    public function limit($limit){
        $this->limit = empty($limit) ? $limit : ' LIMIT '. $limit;
        return $this;
    }

    // 生成选择的SQL语句
    public function select(){
        // 拼装SQL
        $sql = 'SELECT '
            . $this->field // 字段列表
            . ' FROM '
            . $this->table  // 数据表
            . $this->where  // 条件
            . $this->limit;  // 显示数量

        // 预处理
        $stmt = $this->pdo->prepare($sql);
            $stmt->execute();
        return $stmt->fetchAll(\PDO::FETCH_ASSOC);
    }

}

运行实例 »

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

继续上article.php文件

实例

<?php
namespace Article;

require 'Query.php';

class DB{
    /*
     *先定义一个静态属性——连接对象
     *这里定义个静态属性,并且还把的值定义为null,就是方便后面好赋值,好调用
    */
    protected static $pdo = null;

    /* 
    *然后定义一个静态方法——数据库的连接方法
    *注意这里开始调用全局空间的PDO类,传入4个参数并实例化对象:数据库主机host的名称,数据库名称,用户名,登录密码
    */

    public static function connection(){
        self::$pdo = new \PDO('mysql:host=localhost;dbname=phpshouce', 'root', 'root');
    }
    /*
    *继续定义一个魔术方法——静态方法,用来执行方法不存在时的回调

    */

    public static function __callStatic($name, $arguments)
    {
       // 方法不存在,就执行前面的connection()连接数据库
        self::connection();
        // 实例化查询类,将连接对象做为参数
        $query = new Query(self::$pdo);
        // 调用查询对象$query中的对应的方法
        return call_user_func_array([$query, $name],$arguments);
    }
}
$info = DB::table('fubao_user')
->field('id,username,nickname,status')
->where('id > 2')
->limit(5)
->select();
?>

<!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>数据库操作</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($info 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 } ?>
        
</body>
</html>

运行实例 »

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


批改状态:合格

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

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

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