龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

数据库操作类

时间:2014-07-22 14:49来源: 作者: 点击:
分享到:
数据库操作类
数据库操作类
<?php
class DB
{
	
	public $queryNum = 0; // 执行SQL语句的次数 
	public $link; // MySQL连接标识 
	public $dbPrefix;  //表前缀
    /**
     * 缓存实例
     *
     * @var objeact
     * @access protected
     */
    public $cache;
    public $is_cache = false;

	public function __construct($dbhost, $dbuser, $dbpw, $dbname = '', $charset = 'utf_8', $dbPrefix = '')
	{
		$this->link = mysql_connect ( $dbhost, $dbuser, $dbpw, true );
		$this->dbPrefix = $dbPrefix;
		if ($this->link) {
			mysql_select_db ( $dbname, $this->link );
			$charset = str_replace ( '_', '', $charset );
			mysql_query ( "SET NAMES '$charset'", $this->link );
		} else {
			$this->throwException ( 'MySQL server error report!' );
		}
	}
   
	/**
	 * @todo 执行SQL语句
	 * @param string $sql
	 * @param array $args
	 */
	public function query($sql)
    {
        
    	$rs = mysql_query($sql, $this->link);
        if ($rs) {
            $this->queryNum++;
            $this->numRows = mysql_affected_rows($this->link);
            $this->debug($sql);
            return $rs;
        } else {
            $this->debug($sql, false, $this->getError());
            $this->success = false;
            return false;
        }
    }
	
	/**
	 * 返回结果集的数组形式
	 * @return array
	 */
	function fetch_array($rs, $result_type = MYSQL_ASSOC)
	{
		return mysql_fetch_array ( $rs, $result_type );
	}
	
	//	 * 返回结果集的数组形式row
	public function fetch_row($rs)
	{
		return mysql_fetch_row ($rs);
	}
	
	/**
	 * 执行一条SQL语句返回是否成功
	 * @param string $sql  SQL语句
	 * @return boolean
	 */
	public function execute($sql)
    {
        if (mysql_query($sql, $this->link)) {
            $this->queryNum++;
            $this->numRows = mysql_affected_rows($this->link);
            $this->debug($sql);
            return true;
        } else {
            $this->debug($sql, false, $this->getError());
            $this->success = false;
            return false;
        }
    }
	
	/**
	 * 得到结果集的第一个数据
	 * @param string $sql   SQL语句
	 * @return mixed
	 */
	public function getOne($sql)
    {
        if (!$rs = $this->query($sql)) {
            return false;
        }
        $row = $this->fetch($rs);
        $this->free($rs);
        return is_array($row) ? array_shift($row) : $row;
    }
	
	/**
	 * 返回结果集的一行
	 * @param string $sql  SQL语句
	 * @return mixed
	 */
	public function getRow($sql)
    {
        if (!$rs = $this->query($sql)) {
            return false;
        }
        $row = $this->fetch($rs);
        $this->free($rs);
        return $row;
    }
	
	/**
	 * 返回所有结果集
	 * @param string $sql   SQL语句
	 * @param string $limit SQL语句的LIMIT限制
	 * @return mixed
	 */
	public function getAll($sql)
    {
        if (!$rs = $this->query($sql)) {
            return false;
        }
        $all_rows = array();
        while ($rows = $this->fetch($rs)) {
            $all_rows[] = $rows;
        }
        $this->free($rs);
        return $all_rows;
    }
    /**
	 * 取所有行的第一个字段信息
	 * @param string $sql   SQL语句
	 * @return array
	 * @access public
	 */
    public function getCol($sql)
    {
        $res = $this->query($sql);
        if ($res !== false) {
            $arr = array();
            while ($row = mysql_fetch_row($res)) {
                $arr[] = $row[0];
            }

            return $arr;
        } else {
            return false;
        }
    }
	/**
	 * 执行INSERT命令.返回AUTO_INCREMENT
	 * 返回0为没有插入成功
	 * @param string $sql  SQL语句
	 * @return integer
	 */
    public function insert($sql)
    {
        $this->execute($sql);
        return mysql_insert_id($this->link);
    }
	 
	public function insert_id()
	{
		return mysql_insert_id ( $this->link );
	}
/**
     * insert update For Mysql
     * @param <type> $table
     * @param <type> $field_values
     * @param <type> $mode
     * @param <type> $where
     * @return <type>
     */
    public function autoExecute($table, $field_values, $mode = 'INSERT', $where = '')
    {
        $field_names = $this->getCol('DESC ' . $table);

        $sql = '';
        if ($mode == 'INSERT') {
            $fields = $values = array();
            foreach ($field_names AS $value) {
                if (array_key_exists($value, $field_values) == true) {
                    $fields[] = '`' . $value . '`';
                    $values[] = "'" . $field_values[$value] . "'";
                }
            }

            if (!empty($fields)) {
                $sql = 'INSERT INTO ' . $table . ' (' . implode(', ', $fields) . ') VALUES (' . implode(', ', $values) . ')';
            }
        } else {
            $sets = array();
            foreach ($field_names AS $value) {
                if (array_key_exists($value, $field_values) == true) {
                    $sets[] = '`' . $value . "` = '" . $field_values[$value] . "'";
                }
            }

            if (!empty($sets)) {
                $sql = 'UPDATE ' . $table . ' SET ' . implode(', ', $sets) . ' WHERE ' . $where;
            }
        }

        if ($sql) {
            return $this->query($sql);
        } else {
            return false;
        }
    }
	/**
	 * 释放结果集
	 * @param resource $rs 结果集
	 * @return boolean
	 */
	public function free($rs)
	{
		return mysql_free_result ( $rs );
	}
	public function fetch($rs)
    {
        return mysql_fetch_array($rs);
    }
	/**
	 * 关闭数据库
	 *
	 * @access public
	 * @return boolean
	 */
	public function close()
	{
		return mysql_close ( $this->link );
	}
	
	/**
	 * 获取执行SQL语句的个数
	 * @access public
	 * @return integer
	 */
	public function getQueryNum()
	{
		return $this->queryNum;
	}
	
	/**
	 * 获取错误信息
	 * @return void
	 * @access public
	 */
	public function getError()
	{
		echo mysql_errno ( $this->link ) . " : " . mysql_error ( $this->link );
	}
	
	/**
	 * 抛出一个异常信息
	 * @param string $message 异常信息
	 * @return void
	 */
	protected function throwException($message)
	{
		throw new Exception ( $message );
	}
	
	/**
     * DEBUG信息
     * 
     * @param string $sql
     * @param bool $success
     * @param error string
     */
    public function debug($sql, $success = true, $error = null)
    {
        global $TmacConfig;
        if ($TmacConfig['Common']['debug']) {
            $debug = Debug::getInstance();
            $debug->setSQL($sql, $success, $error);
        }
    }
}
精彩图集

赞助商链接