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();
        if ($insert_id)
        {
            if ($insert_info['length'] > 1)
            {
                for ($i = $insert_id; $i < $insert_id + $insert_info['length']; $i++)
                {
                    $id[] = $i;
                }
            }
            else
            {
                /* 添加单条记录 */
                $id = $insert_id;
            }
        }

        return $id;
    }

    /**
     *  获取插入的数据SQL
     *
     *  @author Garbin
     *  @param  array $data
     *  @return string
     */
    function _getInsertInfo($data)
    {
        reset($data);
        $fields = array();
        $values = array();
        $length = 1;
        if (key($data) === 0 && is_array($data[0]))
        {
            $length = count($data);
            foreach ($data as $_k => $_v)
            {
                foreach ($_v as $_f => $_fv)
                {
                    $is_array = is_array($_fv);
                    ($_k == 0 && !$is_array) && $fields[] = '`'.$_f.'`';
                    !$is_array && $values[$_k][] = "'". addslashes(stripslashes($_fv)) . "'";
                }
                $values[$_k] = '(' . implode(',', $values[$_k]) . ')';
            }
        }
        else
        {
            foreach ($data as $_k => $_v)
            {
                $is_array = is_array($_v);
                !$is_array && $fields[] = '`'.$_k.'`';
                !$is_array && $values[] = "'" . addslashes(stripslashes($_v)) . "'";
            }
            $values = '(' . implode(',', $values) . ')';
        }
        $fields = '(' . implode(',', $fields) . ')';
        is_array($values) && $values = implode(',', $values);

        return compact('fields', 'values', 'length');
    }

	//===返回 MySQL 服务器的版本号===
	function get_server_info()
	{
		return @mysql_get_server_info($this->conn);
	}

	//===关闭数据库连接===
	function close()
	{
		return @mysql_close($this->conn);
	}

	//===返回数据库的总记录数===
	function num_rows($query)
	{
		return @mysql_num_rows($query);
	}

	//===返回数据表的总字段数===
	function num_fields($query)
	{
		return @mysql_num_fields($query);
	}

	//===返回根据从结果集取得的行生成的数组===
	function fetch_array($query, $type='MYSQL_BOTH')
	{
		//如果$type='MYSQL_BOTH'时,返回是即是数字数组,又是关联数组
		//如果$type='MYSQL_NUM'时,相当于mysql_fetch_row()函数,返回数字数组
		//如果$type='MYSQL_ASSOC'时,相当于mysql_fetch_assoc()函数,返回关联数组
		return mysql_fetch_array($query, $type);
	}
 }

 

你可能感兴趣的:(mysql)