Blogger Information
Blog 26
fans 1
comment 0
visits 18870
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
链式的数据库
坏人也温柔de陈词滥调
Original
1446 people have browsed it

demo.php

实例

<?php
//使用静态方法的重载
require 'deta.php';
class Db
{
    //用于数据库连接
    protected static $pdo = null;
    // 数据库连接方法
    public static function connection()
    {
        //连接数据的参数
        self::$pdo = new PDO('mysql:host=127.0.0.1;dbname=php','root','root');
    }
    //查询类操作入口
    public static function __callStatic($name, $arguments)
    {
        //创建pdo对象,并连接数据库
        self::connection();
        //实例化查询类,将连接对象作为参数
        $query = new Query(self::$pdo);
        //执行查询类Query中的对象方法
        return call_user_func_array([$query,$name],[$arguments[0]]);
    }
}
//客户端的链式调用
//以Db类做入整数数据库操作的入口,SQL语句的各个部分用对象方法提供
$cats = Db::table('category')
    ->field('cate_id, name ,alias')
    ->where('cate_id >= 2')
    ->limit('2')
    ->select();
//foreach循环查看获取的数据库数据
foreach ($cats as $cat) {
    print_r($cat);
    echo '<br>';
}

运行实例 »

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

deta.php

实例

<?php
//数据库查询类

class deta
{
    //连接对象
    public $pdo = null;
    //字段列表
    public $field = '';
    //数据表名
    public $table = '';
    //查询条件
    public $where = '';
    //显示数量
    public $limit = '';
    //修改值
    public $value = '';

    //构造方法,初始化连接对象
    public function __construct(PDO $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;
    }

    //设置值
    public function value($value)
    {
        $this->limit = $value;
        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);
    }
}

运行实例 »

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


Correction status:qualified

Teacher's comments:写得还行, 如果让你自己写出来估计有难度, 等学完之后, 试着自己写一个模型类来封装常用的操作, 写一个自己的小框架玩
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!