返回顶部

收藏

PHP数据库操作基类(单例模式)

更多

自己练习写的数据库操作基类,包含最基本的CURD操作.可集成到框架内.

数据库操作基类

// 配置文件
<?php
$db = array(
        'host'=>'localhost',
        'user'=>'root',
        'password'=>'',
        'database'=>'test',
)

?>
//php 类
<?php
class db {
    public $conn;
    public static $sql;
    public static $instance=null;
    private function __construct(){
        require_once('db.config.php');
        $this->conn = mysql_connect($db['host'],$db['user'],$db['password']);
        if(!mysql_select_db($db['database'],$this->conn)){
            echo "失败";
        };
        mysql_query('set names utf8',$this->conn);      
    }
    public static function getInstance(){
        if(is_null(self::$instance)){
            self::$instance = new db;
        }
        return self::$instance;
    }
    /**
     * 查询数据库
     */
    public function select($table,$condition=array(),$field = array()){
        $where='';
        if(!empty($condition)){

            foreach($condition as $k=>$v){
                $where.=$k."='".$v."' and ";
            }
            $where='where '.$where .'1=1';
        }
        $fieldstr = '';
        if(!empty($field)){

            foreach($field as $k=>$v){
                $fieldstr.= $v.',';
            }
             $fieldstr = rtrim($fieldstr,',');
        }else{
            $fieldstr = '*';
        }
        self::$sql = "select {$fieldstr} from {$table} {$where}";
        $result=mysql_query(self::$sql,$this->conn);
        $resuleRow = array();
        $i = 0;
        while($row=mysql_fetch_assoc($result)){
            foreach($row as $k=>$v){
                $resuleRow[$i][$k] = $v;
            }
            $i++;
        }
        return $resuleRow;
    }
    /**
     * 添加一条记录
     */
     public function insert($table,$data){
        $values = '';
        $datas = '';
        foreach($data as $k=>$v){
            $values.=$k.',';
            $datas.="'$v'".',';
        }
        $values = rtrim($values,',');
        $datas   = rtrim($datas,',');
        self::$sql = "INSERT INTO  {$table} ({$values}) VALUES ({$datas})";
        if(mysql_query(self::$sql)){
            return mysql_insert_id();
        }else{
            return false;
        };
     }
     /**
      * 修改一条记录
      */
    public function update($table,$data,$condition=array()){
        $where='';
        if(!empty($condition)){

            foreach($condition as $k=>$v){
                $where.=$k."='".$v."' and ";
            }
            $where='where '.$where .'1=1';
        }
        $updatastr = '';
        if(!empty($data)){
            foreach($data as $k=>$v){
                $updatastr.= $k."='".$v."',";
            }
            $updatastr = 'set '.rtrim($updatastr,',');
        }
        self::$sql = "update {$table} {$updatastr} {$where}";
        return mysql_query(self::$sql);
    }
    /**
     * 删除记录
     */
     public function delete($table,$condition){
        $where='';
        if(!empty($condition)){

            foreach($condition as $k=>$v){
                $where.=$k."='".$v."' and ";
            }
            $where='where '.$where .'1=1';
        }
        self::$sql = "delete from {$table} {$where}";
        return mysql_query(self::$sql);

     }

    public static function getLastSql(){
        echo self::$sql;
    }

}

数据库操作基类&nbsp;~&nbsp;114B&nbsp;&nbsp;&nbsp;&nbsp;下载(67)

<?php
$db = array(
        'host'=>'localhost',
        'user'=>'root',
        'password'=>'',
        'database'=>'test',
)

?>

数据库操作基类&nbsp;~&nbsp;3KB&nbsp;&nbsp;&nbsp;&nbsp;下载(77)

<?php
class db {
    public $conn;
    public static $sql;
    public static $instance=null;
    private function __construct(){
        require_once('db.config.php');
        $this->conn = mysql_connect($db['host'],$db['user'],$db['password']);
        if(!mysql_select_db($db['database'],$this->conn)){
            echo "失败";
        };
        mysql_query('set names utf8',$this->conn);      
    }
    public static function getInstance(){
        if(is_null(self::$instance)){
            self::$instance = new db;
        }
        return self::$instance;
    }
    /**
     * 查询数据库
     */
    public function select($table,$condition=array(),$field = array()){
        $where='';
        if(!empty($condition)){

            foreach($condition as $k=>$v){
                $where.=$k."='".$v."' and ";
            }
            $where='where '.$where .'1=1';
        }
        $fieldstr = '';
        if(!empty($field)){

            foreach($field as $k=>$v){
                $fieldstr.= $v.',';
            }
             $fieldstr = rtrim($fieldstr,',');
        }else{
            $fieldstr = '*';
        }
        self::$sql = "select {$fieldstr} from {$table} {$where}";
        $result=mysql_query(self::$sql,$this->conn);
        $resuleRow = array();
        $i = 0;
        while($row=mysql_fetch_assoc($result)){
            foreach($row as $k=>$v){
                $resuleRow[$i][$k] = $v;
            }
            $i++;
        }
        return $resuleRow;
    }
    /**
     * 添加一条记录
     */
     public function insert($table,$data){
        $values = '';
        $datas = '';
        foreach($data as $k=>$v){
            $values.=$k.',';
            $datas.="'$v'".',';
        }
        $values = rtrim($values,',');
        $datas   = rtrim($datas,',');
        self::$sql = "INSERT INTO  {$table} ({$values}) VALUES ({$datas})";
        if(mysql_query(self::$sql)){
            return mysql_insert_id();
        }else{
            return false;
        };
     }
     /**
      * 修改一条记录
      */
    public function update($table,$data,$condition=array()){
        $where='';
        if(!empty($condition)){

            foreach($condition as $k=>$v){
                $where.=$k."='".$v."' and ";
            }
            $where='where '.$where .'1=1';
        }
        $updatastr = '';
        if(!empty($data)){
            foreach($data as $k=>$v){
                $updatastr.= $k."='".$v."',";
            }
            $updatastr = 'set '.rtrim($updatastr,',');
        }
        self::$sql = "update {$table} {$updatastr} {$where}";
        return mysql_query(self::$sql);
    }
    /**
     * 删除记录
     */
     public function delete($table,$condition){
        $where='';
        if(!empty($condition)){

            foreach($condition as $k=>$v){
                $where.=$k."='".$v."' and ";
            }
            $where='where '.$where .'1=1';
        }
        self::$sql = "delete from {$table} {$where}";
        return mysql_query(self::$sql);

     }

    public static function getLastSql(){
        echo self::$sql;
    }

}

$db = db::getInstance();
//$list = $db->select('demo',array('name'=>'tom','password'=>'ds'),array('name','password'));
//echo $db->insert('demo',array('name'=>'最近你啦','password'=>'123'));
//echo $db->update('demo',array("name"=>'xxx',"password"=>'123'),array('id'=>1));
echo $db->delete('demo',array('id'=>'2'));
db::getLastSql();
echo "<pre>";
?>

标签:PHP,MySQL

收藏

0人收藏

支持

0

反对

0

相关聚客文章
  1. 青鱼 发表 2016-04-28 07:44:00 对PHP连接MySQL和其他数据库方式的理解
  2. colinhu 发表 2012-12-02 08:47:07 LAMP架构相关书籍介绍
  3. lixiphp 发表 2013-06-07 07:32:43 HTML字符编码JS和PHP 转义
  4. aquan 发表 2012-12-29 09:32:23 BuyVM VPS性能测试
  5. 郑永 发表 2013-09-20 00:34:38 手机搭建php+mysql完美运行wordpress
  6. Li Xi 发表 2014-02-24 14:20:35 PHP 连接 unix_socket MySQL
  7. enjoy <> 发表 2013-11-17 13:57:48 php5.4后htmlspecialchars输出为空的问题
  8. Cherrot 发表 2012-11-10 09:37:29 编译配置LAMP环境
  9. Cherrot 发表 2014-11-02 15:40:10 VPS初始配置+LNMP环境速配指南
  10. wkii 发表 2013-09-16 15:11:52 Php Aes加密类
  11. ISADBA|FH.CN 发表 2014-12-18 01:29:59 安装MySQL慢查询日志工具Anemometer
  12. 青鱼 发表 2016-04-28 07:44:00 对PHP连接MySQL和其他数据库方式的理解

发表评论