日期:2014-05-16  浏览次数:20986 次

mysql连接数据库的基本类文件

 /*
 * Created on 2010-10-21
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 class mysql_class
 {
 	private $host;
 	private $user;
 	private $pwd;
 	private $data;
 	private $charset;
 	public  $conn;

	//=====构造函数,用于初始化=====
 	function __construct($host, $user, $pwd, $data, $charset)
 	{
 		$this->host = $host;
 		$this->user = $user;
 		$this->pwd  = $pwd;
 		$this->data = $data;
 		$this->charset   = $charset;
 		$this->connect();
 	}

 	//====连接数据库====
 	function connect()
 	{
        $this->conn = @mysql_connect($this->host, $this->user, $this->pwd) or die("连接数据库错误!<br>".$this->error());
        mysql_select_db($this->data, $this->conn) or die("该数据库(".$this->data.")不存在!");
        mysql_query("set names ".$this->charset);
 	}

 	//===查询数据表===
 	function query($sql, $type='')
 	{
 		if(!$query = mysql_query($sql, $this->conn)) $this->show("error:", $sql);
		return $query;
 	}

 	//===提示查询数据表错误信息===
 	function show($message='', $sql='')
 	{
		if(!$sql) echo "{$message}查询语句不能为空";
		else echo $message."".$sql;
 	}

 	//===返回前一次 MySQL 操作所影响的记录总行数===
    function affected_rows() {
		return mysql_affected_rows();
	}

	//===返回$row条记录的第$fields个字段值($fields可以是数字或字段名称)===
	function result($query, $row=0, $fields=0)
	{
		$row = abs($row);
		if(is_numeric($fields)) $fields = abs($fields);
		if($row >= $this->num_rows($query)) $row = $this->num_rows($query);
		if($fields >= $this->num_fields($query)) $fields = $this->num_fields($query);
		return mysql_result($query,$row,$fields);
	}

	//===返回上一步 INSERT 操作产生的 ID值===
	function insert_id()
	{
		return mysql_insert_id($this->conn);
	}

	//===释放内存空间===
	function free_result($query)
	{
		return mysql_free_result($query);
	}

    /**
     * 用于统计
     */
    function getAll($sql)
    {
        $res = $this->query($sql);
        if ($res !== false)
        {
            $arr = array();
            while ($row = mysql_fetch_assoc($res))
            {
                $arr[] = $row;
            }
            return $arr;
        }
        else
        {
            return false;
        }
    }

    function getRow($sql, $limited = false)
    {
        if ($limited == true)
        {
            $sql = trim($sql . ' LIMIT 1');
        }

        $res = $this->query($sql);
        if ($res !== false)
        {
            return mysql_fetch_assoc($res);
        }
        else
        {
            return false;
        }
    }

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

    function getOne($sql, $limited = false)
    {
        if ($limited == true)
        {
            $sql = trim($sql . ' LIMIT 1');
        }

        $res = $this->query($sql);
        if ($res !== false)
        {
            $row = mysql_fetch_row($res);
            if ($row !== false)
            {
                return $row[0];
            }
            else
            {
                return '';
            }
        }
        else
        {
            return false;
        }
    }

    /**
     *  添加一条记录
     *
     *  @author Garbin
     *  @param  array $data
     *  @return mixed
     */
    function add($table, $data, $compatible = false)
    {
        $id = '';
        if (empty($data))
        {
            return false;
        }
        $insert_info = $this->_getInsertInfo($data);
        $mode = $compatible ? 'REPLACE' : 'INSERT';

        $this->query("{$mode} INTO {$table} {$insert_info['fields']} VALUES{$insert_info['values']}");
        $insert_id = $this->insert_id();