PDO数据库操作类

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;   
    }   
}

标签: SQL, dbh, condition, sth