PHP连接多个mysql数据库的类

2012年12月23日 发表评论 阅读评论

转载请注明出处!


header('Content-Type:text/html;charset=utf-8');
/*
 * 定义一个连接类,可以访问建立多个数据库连接对象
 *
 * author 寞踪 http://weibo.com/xpmozong
 * http://xpmyblog.sinaapp.com/?p=175
 *
 */
class mysql{
	var $dbhost;		//数据库地址
	var $dbuser;		//数据库用户名
	var $dbpasswd;		//数据库密码
	var $dbpconnect=0;	//数据库长连接
	var $dbname;		//数据库名称
	var $dbchart;		//数据库链接编码
	var $dblink;		//数据库连接对象
	var $sql;			//sql语句
	var $res;			//sql语句执行结果
	var $errno;			//错误信息		

	function connect($dbConfig) {
		$this->dbhost = $dbConfig['dbhost'];
		$this->dbuser = $dbConfig['dbuser'];
		$this->dbpasswd = $dbConfig['dbpasswd'];
		$this->dbpconnect = $dbConfig['dbpconnect'];
		$this->dbname = $dbConfig['dbname'];
		$this->dbchart = $dbConfig['dbchart'];
		if($this->dbpconnect) {
			$this->dblink = mysql_pconnect($this->dbhost,$this->dbuser,$this->dbpasswd,1) or die('can not connect to mysql database!');
		} else {
			$this->dblink = mysql_connect($this->dbhost,$this->dbuser,$this->dbpasswd,1) or die('can not connect to mysql database!');
		}
		mysql_query('set names '.$this->dbchart, $this->dblink);
		mysql_select_db($this->dbname, $this->dblink);
		unset($dbConfig);
	}

	/**
	 * 数据库执行语句
	 *
	 * @return blooean
	 *
	 */
	function query($sql, $die_msg = 1) {
		$this->sql = $sql;
		$result = @mysql_query($sql, $this->dblink); //可以用自定义错误信息的方法,就要压制本身的错误信息
		if($result == true) {
			return $result;
		}else{
			//有错误发生
			$this->errno = mysql_errno($this->dblink);
			if($this->errno >0) {
				if($die_msg == 1) {
					//强制报错并且die
					$this->msg();
				}else{
					return $this->errno;//无强制报错,则返回错误代码
				}
			}
		}
	}

	/**
	 * 获得查询语句单条结果
	 *
	 * @return array
	 *
	 */
	function getOne($sql) {
		$this->sql = $sql;
		$this->res = $this->query($sql);
		return mysql_fetch_assoc($this->res);
	}

	/**
	 * 获得查询语句多条结果
	 *
	 * @return array
	 *
	 */
	function getAll($sql) {
		$this->sql = $sql;
		$this->res = $this->query($sql);
		$arr = array();
		while($row = mysql_fetch_assoc($this->res)) {
			$arr[] = $row;
		}
		return $arr;
	}

	/**
	 * 取得结果数据
	 *
	 * @param resource $query
	 *
	 * @return string
	 *
	 */
	function result($query, $row) {
		$query = @mysql_result($query, $row);
		return $query;
	}

	/**
	 * 获得刚插入数据的id
	 *
	 * @return int id
	 *
	 */
	function getInsertID() {
		return ($id = mysql_insert_id($this->dblink)) >= 0 ? $id : $this->result($this->query('SELECT last_insert_id()'), 0);
	}

	/**
	 * 关闭数据库连接,当您使用持续连接时该功能失效
	 *
	 * @return blooean
	 *
	 */
	function close() {
		return mysql_close($this->dblink);
	}

	/**
	 * 显示自定义错误
	 *
	 */
	function msg() {
		if($this->errno) {
			//可以根据错误ID,配置好一些友好的错误信息提示语句
			$msgArr = array();
			$msgArr['1062'] = "唯一性索引有重复值插入";
			/*...更多错误代码根据实际业务再添加...*/
			if($msgArr[$this->errno]) {
				$errMsg = $msgArr[$this->errno];//已定义的错误
			}else{
				$errMsg = mysql_error();//未定义的错误,由默认的错误信息决定
			}
			echo "
\n"; echo "

数据库操作错误

\n"; echo "
错误代码:".$this->errno."
\n"; echo "
错误信息:".$errMsg."
\n"; echo "
"; unset($msgArr); die; } } } $dbConfig1 = array('dbhost'=>'192.168.0.104','dbuser'=>'root','dbpasswd'=>'123456','dbname'=>'child','dbpconnect'=>0,'dbchart'=>'utf8'); $dbConfig2 = array('dbhost'=>'192.168.0.104','dbuser'=>'root','dbpasswd'=>'123456','dbname'=>'shop','dbpconnect'=>0,'dbchart'=>'utf8'); $db1 = new mysql(); $db1->connect($dbConfig1); $db2 = new mysql(); $db2->connect($dbConfig2); $res1 = $db1->getAll('select * from user_info limit 2'); echo "
";
print_r($res1);

$res2 = $db2->getAll('select * from ecs_users limit 2');
echo "
";
print_r($res2);


转载请注明来自:[MSN Spaces]http://msn.shandian.biz/468.html

  1. 本文目前尚无任何评论.