• 沙里软件

  • ShaliSoft.com [手机站]   办公桌收纳抽屉
  • 首页
  • 博文
  • 演示
  • 管理
  • PDO数据库操作类

    网络   2013/9/25 17:34:21

    PHP代码
    <?php    

    /**  
    * 功能:数据库操作类  
    * 作者:phpox  
    * 日期:Tue Aug 14 08:46:27 CST 2007  
    */  

    defined('PHPOX') or die(header("HTTP/1.1 403 Not Forbidden"));    

    class include_database    
    {    
       private static $instance;    
       public $dsn;    
       public $dbuser;    
       public $dbpass;    
       public $sth;    
       public $dbh;    

       private function __construct()    
       {    
           $this->dsn = 'mysql:host='.DB_HOST.';port='.DB_PORT.';dbname='.DB_NAME;    
           $this->dbuser = DB_USER;    
           $this->dbpass = DB_PASS;    
           $this->connect();    
           $this->dbh->query('SET NAMES '.DB_CHARSET);    
       }    

       public static function getInstance()    
       {    
           if (self::$instance === null)    
           {    
               self::$instance = new include_database();    
           }    
           return self::$instance;    
       }    

       //连接数据库    
       private function connect()    
       {    
           try    
           {    
               $this->dbh = new PDO($this->dsn,$this->dbuser,$this->dbpass);    
           }    
           catch (PDOException $e)    
           {    
               exit('连接失败:'.$e->getMessage());    
           }    
       }    

       //获取数据表里的字段    
       public function getFields($table)    
       {    
           $this->sth = $this->dbh->query("DESCRIBE $table");    
           $this->getPDOError();    
           $this->sth->setFetchMode(PDO::FETCH_ASSOC);    
           $result = $this->sth->fetchAll();    
           $this->sth = null;    
           return $result;    
       }    

       //获取要操作的数据    
       private function getCode($table,$args)    
       {    
           $allTables = require_once(DOCUMENT_ROOT.'/cache/tables.php');    
           if (!is_array($allTables[$table]))    
           {    
               exit('表名错误或未更新缓存!');    
           }    
           $tables = array_flip($allTables[$table]);    
           $unarr = array_diff_key($args,$tables);    
           if (is_array($unarr))    
           {    
               foreach ($unarr as $k => $v)    
               {    
                   unset($args[$k]);    
               }    
           }    
           $code = '';    
           if (is_array($args))    
           {    
               foreach ($args as $k => $v)    
               {    
                   if ($v == '')    
                   {    
                       continue;    
                   }    
                   $code .= "`$k`='$v',";    
               }    
           }    
           $code = substr($code,0,-1);    
           return $code;    
       }    

       //插入数据    
       public function insert($table,$args,$debug = null)    
       {    
           $sql = "INSERT INTO `$table` SET ";    
           $code = $this->getCode($table,$args);    
           $sql .= $code;    
           if ($debug)echo $sql;    
           if ($this->dbh->exec($sql))    
           {    
               $this->getPDOError();    
               return $this->dbh->lastInsertId();    
           }    
           return false;    
       }    

       //查询数据    
       public function fetch($table,$condition = '',$sort = '',$limit = '',$field = '*',$debug = false)    
       {    
           $sql = "SELECT {$field} FROM `{$table}`";    
           if (false !== ($con = $this->getCondition($condition)))    
           {    
               $sql .= $con;    
           }    
           if ($sort != '')    
           {    
               $sql .= " ORDER BY $sort";    
           }    
           if ($limit != ''){    
               $sql .= " LIMIT $limit";    
           }    
           if ($debug)echo $sql;    
           $this->sth = $this->dbh->query($sql);    
           $this->getPDOError();    
           $this->sth->setFetchMode(PDO::FETCH_ASSOC);    
           $result = $this->sth->fetchAll();    
           $this->sth = null;    
           return $result;    
       }    

       //查询数据    
       public function fetchOne($table,$condition = null,$field = '*',$debug = false)    
       {    
           $sql = "SELECT {$field} FROM `{$table}`";    
           if (false !== ($con = $this->getCondition($condition)))    
           {    
               $sql .= $con;    
           }    
           if ($debug)echo $sql;    
           $this->sth = $this->dbh->query($sql);    
           $this->getPDOError();    
           $this->sth->setFetchMode(PDO::FETCH_ASSOC);    
           $result = $this->sth->fetch();    
           $this->sth = null;    
           return $result;    
       }    

       //获取查询条件    
       public function getCondition($condition='')    
       {    
           if ($condition != '')    
           {    
               $con = ' WHERE';    
               if (is_array($condition))    
               {    
                   $i = 0;    
                   foreach ($condition as $k => $v)    
                   {    
                       if ($i != 0){    
                           $con .= " AND $k = '$v'";    
                       }else {    
                           $con .= " $k = '$v'";    
                       }    
                       $i++;    
                   }    
               }elseif (is_string($condition))    
               {    
                   $con .= " $condition";    
               }else {    
                   return false;    
               }    
               return $con;    
           }    
           return false;    
       }    

       //获取记录总数    
       public function counts($table,$condition = '',$debug = false)    
       {    
           $sql = "SELECT COUNT(*) AS num FROM `$table`";    
           if (false !== ($con = $this->getCondition($condition)))    
           {    
               $sql .= $con;    
           }    
           if ($debug) echo $sql;    
           $count = $this->dbh->query($sql);    
           $this->getPDOError();    
           return $count->fetchColumn();    
       }    

       //按SQL语句查询    
       public function doSql($sql,$model='many',$debug = false)    
       {    
           if ($debug)echo $sql;    
           $this->sth = $this->dbh->query($sql);    
           $this->getPDOError();    
           $this->sth->setFetchMode(PDO::FETCH_ASSOC);    
           if ($model == 'many')    
           {    
               $result = $this->sth->fetchAll();    
           }else {    
               $result = $this->sth->fetch();    
           }    
           $this->sth = null;    
           return $result;    
       }    

       //修改数据    
       public function update($table,$args,$condition,$debug = null)    
       {    
           $code = $this->getCode($table,$args);    
           $sql = "UPDATE `$table` SET ";    
           $sql .= $code;    
           if (false !== ($con = $this->getCondition($condition)))    
           {    
               $sql .= $con;    
           }    
           if ($debug)echo $sql;    
           if (($rows = $this->dbh->exec($sql)) > 0)    
           {    
               $this->getPDOError();    
               return $rows;    
           }    
           return false;    
       }    

       //字段递增    
       public function increase($table,$condition,$field,$debug=false)    
       {    
           $sql = "UPDATE `$table` SET $field = $field + 1";    
           if (false !== ($con = $this->getCondition($condition))){    
               $sql .= $con;    
           }    
           if ($debug)echo $sql;    
           if (($rows = $this->dbh->exec($sql)) > 0){    
               $this->getPDOError();    
               return $rows;    
           }    
           return false;    
       }    

       //删除记录    
       public function del($table,$condition,$debug = false)    
       {    
           $sql = "DELETE FROM `$table`";    
           if (false !== ($con = $this->getCondition($condition)))    
           {    
               $sql .= $con;    
           }else {    
               exit('条件错误!');    
           }    
           if ($debug)echo $sql;    
           if (($rows = $this->dbh->exec($sql)) > 0)    
           {    
               $this->getPDOError();    
               return $rows;    
           }else {    
               return false;    
           }    
       }    

       /**  
        * 执行无返回值的SQL查询  
        *  
        */  
       public function execute($sql)    
       {    
           $this->dbh->exec($sql);    
           $this->getPDOError();    
       }    

       /**  
        * 捕获PDO错误信息  
        */  
       private function getPDOError()    
       {    
           if ($this->dbh->errorCode() != '00000')    
           {    
               $error = $this->dbh->errorInfo();    
               exit($error[2]);    
           }    
       }    

       //关闭数据连接    
       public function __destruct()    
       {    
           $this->dbh = null;    
       }    
    }

    阅读(5574) 分享(0)

    上一篇: PHP中Session()函数使用
    下一篇: PHP 生成缩略图的类

  • 精彩推荐

    ◆ 安装完office后 在组件服务里DCOM配置中找不到
    ◆ 微信清缓存工具,微信怎么清理缓存?
    ◆ 用回溯法解决子集和问题【C#版本】
    ◆ 实测什么物体会影响WIFI信号
    ◆ 利用UC微信分享接口进行WEB微信分享
    ◆ ASP.NET之GridView Eval() 中数据格式化或格式化数据
    ◆ css常用hack语法
    ◆ 面向对象的缺点,你了解了吗
    ◆ 我国首台可人脸识别ATM机发布 不刷脸不能取钱
    ◆ 2G网络要关闭了吗?你还不打算换4G手机?
  • 用心做事 不能唯利是图

    • 吊儿
    • 用QQ联系我17905772
  • 搜索


  • 最新文章

    • 导出Excel 格式 mso-number-format
    • 服务器iis支持tls1.2,windows server 2008 r2 中IIS启用TLS 1.2(安装SSL后用TLS 1.2)
    • MySQL配置优化
    • EditPlus 添加文件比较工具winmerge
    • 滚动悬浮固定JS特效

  • 热门文章

    • php sso单点登录实现代码
    • 中国菜刀(China chopper) 最新黑客工具
    • redis.conf中文版(基于2.4)
    • 搜索引擎名单大全
    • php图片上传类,支持加水印,生成略缩图

  • 最新图库


  • 最新评论


  • 友情链接

  • 沙里软件

  • 最近访客

    Powered by ShaliSoft.com 豫ICP备13008529号

    免责声明:本站部分内容来源于互联网,转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,不为其版权负责,也不构成任何其他建议。如果发现侵犯版权,联系QQ17905772进行删除。