Blogger Information
Blog 41
fans 2
comment 0
visits 29710
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php之数据库链式操作
月光下,遗忘黑暗
Original
1126 people have browsed it

代码块

<?php/** * 事件委托 : 数据库查询构造器 * Db::table()->field()->where()->limit()->select()->toArray(); *  */ //被委托的类class Query{    protected $db;//pdo连接对象    protected $table;    protected $field;    protected $limit;    protected $value;    //连接数据库    private function connect($dsn,$username,$password)    {        $this->db = new PDO($dsn,$username,$password);    }    function __construct($dsn,$username,$password)    {        $this->connect($dsn,$username,$password);    }    //数据表方法    public function table($table)    {        $this->table = $table;        return $this;    }    public function field($field)    {        $this->field = $field;        return $this;    }    public function limit($limit)    {        $this->limit = $limit;        return $this;    }    public function value($value)    {        $this->value = $value;        return $this;    }    public function getSql()    {        return sprintf('INSERT INTO %s (%s) VALUE(%s)',$this->table,$this->field,$this->value);    }    //执行查询    public function select()    {        return $this->db->query($this->getSql())->fetchAll(PDO::FETCH_ASSOC);    }    public function insert()    {         return $this->db->exec($this->getSql());     }}//工作类class Db{    static function __callStatic($method,$args)    {        $dsn = 'mysql:host=localhost;dbname=video';        $username = 'root';        $password = 'root';        $query = new Query($dsn,$username,$password);        //直接委托给Query类中的具体方法完成        return call_user_func([$query,$method],...$args);    }}$res = Db::table('admins')->field('username,password,truename,gid,add_time')->value("'aaa',321,'fdsaf',1,52342")->insert();echo '<pre>';print_r($res);

效果

Correcting teacher:灭绝师太灭绝师太

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