PHP高级技能

php数据库单例类链式操作

2018-02-27  本文已影响20人  AISpider

直接上代码:

<?php 
/**
* 数据库连接类 mysqli连接模式
*/
class DB
{
    static public $instance;
    private $sql=array(
            "field"=>" * ",
            "from"=>"",
            "where"=>"",
            "order"=>"",
            "limit"=>"");
    //初始化 防止new 继承。
    private function __construct($tab_name="") 
    {
        $this->test='13213';
        $this->debug=true;
        $this->connect_db();
    }
    // 连接数据库 静态,安全
    private function connect_db()
    {
        $this->connect = mysqli_connect('127.0.0.1','root','','usite') or die('The database connection is failed:'.mysqli_connect_error());
        $this->db_log('The database connection is successful');
    }
    
    // static  public为提供外部静态访问,
    static public function instance()
    {
        if (!self::$instance) {
            self::$instance=new self;
        }
        return self::$instance;
    }

    // 内部输出
    private function db_log($log)
    {
        if ($this->debug) {
            echo $log;
        }
    }

    //获取最近执行的sql语句
    public function get_sql()
    {   
        if ($this->sql) {
            return $this->sql;
        }
        else{
            return false;
        }
    }

    public function field($field="*")
    {
        $this->sql['field']=' '.$field;
        return $this;    
    }

    public function from($table_name)
    {
        
        
        if (isset($this->sql['from'])) {
            $this->sql['from']='FROM '.$table_name;
        }
        return $this;
    }

    // 目前参数为数组,只支持条件为and,如果需要where的,请使用字符代替。
    public function where($map)
    {
        if (!$map) {
            return $this;
        }
        elseif (is_array($map)) {
            $where_str=" ";
            foreach ($map as $key => $row) {
                $where_str=$where_str.'`'.$key.'`="'.$row.'" and ';
            }
            $where_str=$where_str.'1';
            $this->sql['where']='WHERE '.$where_str;
        }
        else{
            $this->sql['where']='WHERE '.$map;
        }
        return $this;
    }

    public function order($order)
    {
        $this->sql['order']='ORDER BY '.$order;
        return $this;
    }

    public function limit($limit)
    {
        $this->sql['limit']='LIMIT '.$limit;
        return $this;
    }

    public function get_one()
    {
        $sql="SELECT * FROM us_user limit 0,1";
        if ($result=mysqli_query($this->connect,$sql)){
            $row=mysqli_fetch_assoc($result);
            return $row;
        }
        else{
            return false;
        }
    }
    // 查询结果集
    public function query($sql)
    {
        # code...
    }

    public function select()
    {
        $this->sql= "SELECT ".(implode(" ",$this->sql));
    }
}

function M($table_name)
{
    $db=DB::instance(); //正确
    return $db->from($table_name);
}
$map=array();
$map['id']=1;
$map['user_name']="zhangsan";
echo "<hr />";
$res=M('us_user')->field('id')->where($map)->order('id desc')->limit('0,1')->select();
echo M('us_user')->get_sql();

执行结果:

SELECT id FROM us_user WHERE `id`="1" and `user_name`="zhangsan" and 1 ORDER BY id desc LIMIT 0,1
上一篇 下一篇

猜你喜欢

热点阅读