数据库操作类,链式操作

<?php

define('MY_DBHOST', 'localhost');

define('MY_PORT', '3306');

define('MY_DBUSER', 'root');

define('MY_DBPW', '123456');

define('MY_DBNAME', 'test');

define('MY_DBCHARSET', 'utf8');

define('MY_DBTABLEPRE', 'pre_');

function M($table,$database='',$pre=''){

    if(empty($database)){

        $db=new MYSQL(MY_DBHOST,MY_PORT,MY_DBUSER,MY_DBPW,MY_DBNAME,MY_DBTABLEPRE,MY_DBCHARSET);

    }

    else{

        $db=new MYSQL(MY_DBHOST,MY_PORT,MY_DBUSER,MY_DBPW,$database,$pre,MY_DBCHARSET);

    }

    return $db->table($table);

}

/**

 * MySQL 数据库操作工具类, 方便数据库操作.

 */

class MYSQL{



    private     $conn;

    private  $pre;

    private  $where;

    private  $limit;

    private  $order;

    private  $field;

    public function __construct($host,$port,$user,$pw,$db,$pre,$charset){

        if(!$this->conn){

            $server = $host.':'.$port;

            $this->conn = mysql_connect($server, $user, $pw, true) or die('connect db error');

            $this->pre=$pre;

            mysql_select_db($db,$this->conn) or die('select db error');

            if($charset){

                mysql_query("set names " . $charset, $this->conn);

            }

            

        }

    }

    public function __destruct(){

        if($this->conn){

            mysql_close($this->conn);

        }

    }

    /*

        快捷找到一行记录

    */

    public function find(){

        $result=$this->query($this->sql());

        if(!$result){

            return false;

        }

        else{

            return $result[0];

        }

    }

    /*

        选择所有的资源

    */

    public function select(){



        return $this->query($this->sql());

    }

    /*

        获取某行记录的某个字段

    */

    public function getField($field){

        $this->field=$field;

        $result=$this->query($this->sql());

        if(!$result){

            return false;

        }

        return $result[0][$field];

    }

    /*

        设置某行记录的某个字段

    */

    public function setField($field,$value){

        $sql='update '.$this->table.' set `'.$field.'`='.'\''.$value.'\' '.$this->where;

        $this->execute($sql);

    }

    /*

        统计

    */

    public function count(){

        $sql='select count(*) as ask_count from '.$this->table.' '.$this->where.' limit 0,1';

        $result=$this->query($sql);

        return $result[0]['ask_count'];

    }

    public function sum($field){

        $sql='select sum('.$field.') as ask_sum from '.$this->table.' '.$this->where;

        $result=$this->query($sql);

        return $result[0]['ask_sum'];

    }

    /*

    添加记录

    */

    public function add($data){

        $fields=array();

        $values=array();

        foreach($data as $k=>$v){

            $fields[]='`'.$k.'`';

            $values[]='\''.$v.'\'';

        }

        $sql="insert ".$this->table.' ('.implode(',',$fields).') values ('.implode(',',$values).')';

        $this->execute($sql);

        return mysql_insert_id($this->conn);



    }

    /*

    更新添加记录

    */

    public function readd($data){

        $fields=array();

        $values=array();

        foreach($data as $k=>$v){

            $fields[]='`'.$k.'`';

            $values[]='\''.$v.'\'';

        }

        $sql="replace into ".$this->table.' ('.implode(',',$fields).') values ('.implode(',',$values).')';

        $this->execute($sql);

        return mysql_insert_id($this->conn);



    }

    /*

    更新记录

    */

    public function save($data){

        $new=array();

        foreach($data as $k=>$v){

            $new[]='`'.$k.'`=\''.$v.'\'';

        }

        $sql='update '.$this->table.' set '.implode(',',$new).' '.$this->where;

        $this->execute($sql);

    }

    /*

    删除记录

    */

    public function delete(){

        $sql='delete from '.$this->table.' '.$this->where;

        $this->execute($sql);

    }

    /*

        执行原生的SQL

    */

    public function query($sql){

        $result=mysql_query($sql,$this->conn);

        if($result === false){

            return false;

        }

        if(mysql_num_rows($result)==0){

            return false;

        }

        

        $data=array();

        while($row=mysql_fetch_assoc($result)){

            $data[]=$row;

        }

        $this->resetsql();

        return $data;

    }

    /*

        生成SQL并执行返回结果资源

    */

    public function execute($sql){

        mysql_query($sql,$this->conn);

        $this->resetsql();

    }

    /*

        设置where

    */

    public function where($map){

        $this->where='where '.$map;

        return $this;

    }

    public function error(){

        return mysql_error($this->conn);

    }

    /*

        设置order

    */

    public function order($order){

        $this->order='order by '.$order;

        return $this;

    }

    /*

        设置limit

    */

    public function limit($limit){

        if(is_numeric($limit)){

            $this->limit='limit 0,'.$limit;    

        }

        else{

            $this->limit='limit '.$limit;    

        }

        return $this;

    }

    /*

        设置field

    */

    public function field($field){

        $this->field=$field;

        return $this;

    }

    /*

        设置table

    */

    public function table($table){

        $this->table=$this->pre.$table;

        return $this;

    }

    /*

        生成查询sql

    */

    public function sql(){

        $sql='select';

        if($this->field){

            $sql.=' '.$this->field.' from';

        }

        else{

            $sql.=' * from';

        }

        $sql.=' '.$this->table;

        if($this->where){

            $sql.=' '.$this->where;

        }

        if($this->order){

            $sql.=' '.$this->order;

        }

        if($this->limit){

            $sql.=' '.$this->limit;

        }

        //echo $sql.'<br>';

        return $sql;

    }

    /*

    查询完毕以后清空

    */

    Public function resetsql(){

        $this->field='';

        $this->order='';

        $this->limit='';

        $this->where='';

    }

    /*

        解析MYSQL的返回结果

    */

    private function parseResult($result,$find=false,$reset=true){

        if(mysql_num_rows($result)==0){

            return false;

        }

        

        if($find){

            return mysql_fetch_assoc($result); 

        }

        $data=array();

        while($row=mysql_fetch_assoc($result)){

            $data[]=$row;

        }

        if($reset){

            $this->resetsql();

        }

        return $data;

    }

}

?>

 

你可能感兴趣的:(数据库)