PHP Mysql连接数据库基类


Mysql.class.php:

<?php

class Mysql{
	protected $conn = false;  //数据库连接资源
	protected $sql;           //sql语句
	
	/**
	 * 构造函数,负责连接服务器、选择数据库、设置字符集等
	 * @param $config string 配置数组
	 */
	public function __construct($config = array()){
		$host = isset($config['host'])? $config['host'] : 'localhost';
		$user = isset($config['user'])? $config['user'] : 'root';
		$password = isset($config['password'])? $config['password'] : '';
		$dbname = isset($config['dbname'])? $config['dbname'] : '';
		$port = isset($config['port'])? $config['port'] : '3306';
		$charset = isset($config['charset'])? $config['charset'] : 'utf8';
		
		$this->conn = mysql_connect("$host:$port",$user,$password) or die('数据库连接错误');
		mysql_select_db($dbname) or die('数据库选择错误');
		$this->setChar($charset);
	}

	/**
	 * 设置字符集
	 * @access private
	 * @param $charset string 字符集
	 */
	private function setChar($charest){
		$sql = 'set names '.$charest;
		$this->query($sql);
	}

	/**
	 * 执行sql语句
	 * @access public
	 * @param $sql string 查询sql语句
	 * @return $result,成功返回资源,失败则输出错误信息,并退出
	 */
	public function query($sql){		
		$this->sql = $sql;
		$result = mysql_query($this->sql,$this->conn);
		
		if (! $result) {
			die($this->errno().':'.$this->error().'<br />出错语句为'.$this->sql.'<br />');
		}
		return $result;
	}

	/**
	 * 获取第一条记录的第一个字段
	 * @access public
	 * @param $sql string 查询的sql语句
	 * @return 返回一个该字段的值
	 */
	public function getOne($sql){
		$result = $this->query($sql);
		$row = mysql_fetch_row($result);
		if ($row) {
			return $row[0];
		} else {
			return false;
		}
	}

	/**
	 * 获取一条记录
	 * @access public
	 * @param $sql 查询的sql语句
	 * @return array 关联数组
	 */
	public function getRow($sql){
		if ($result = $this->query($sql)) {
			$row = mysql_fetch_assoc($result);
			return $row;
		} else {
			return false;
		}
	}

	/**
	 * 获取所有的记录
	 * @access public 
	 * @param $sql 执行的sql语句
	 * @return $list 有所有记录组成的二维数组
	 */
	public function getAll($sql){
		$result = $this->query($sql);
		$list = array();
		while ($row = mysql_fetch_assoc($result)){
			$list[] = $row;
		}
		return $list;
	}

	/**
	 * 获取某一列的值
	 * @access public
	 * @param $sql string 执行的sql语句
	 * @return $list array 返回由该列的值构成的一维数组
	 */
	public function getCol($sql){
		$result = $this->query($sql);
		$list = array();
		while ($row = mysql_fetch_row($result)) {
			$list[] = $row[0];
		}
		return $list;
	}

	
	/**
	 * 获取上一步insert操作产生的id
	 */
	public function getInsertId(){
		return mysql_insert_id($this->conn);
	}
	/**
	 * 获取错误号
	 * @access private
	 * @return 错误号
	 */
	public function errno(){
		return mysql_errno($this->conn);
	}

	/**
	 * 获取错误信息
	 * @access private
	 * @return 错误private信息
	 */
	public function error(){
		return mysql_error($this->conn);
	}

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值