2012-01-06 PHP连接多个mysql数据库的类 mysql.php类 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161/* * 定义一个连接类,可以访问建立多个数据库连接对象 * */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_error($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 "<div style='color:red;'>\n"; echo "<h4>数据库操作错误</h4>\n"; echo "<h5>错误代码:".$this->errno."</h5>\n"; echo "<h5>错误信息:".$errMsg."</h5>\n"; echo "</div>"; unset($msgArr); die; } } function __destruct() { $this->dbhost; $this->dbuser; $this->dbpasswd; $this->dbpconnect; $this->dbname; $this->dbchart; $this->dblink; $this->sql; $this->res; $this->errno; }} 测试连接 12345678910111213$dbConfig1 = array( 'dbhost' => 'localhost', 'dbuser' => 'root', 'dbpasswd' => '', 'dbname' => 'server', 'dbpconnect' => 0, 'dbchart' => 'utf8');$db1 = new mysql();$db1->connect($dbConfig1);$arr = $db1->getOne("select * from user");print_r($arr);$db1->close(); Newer PHP之call_user_func_array用法 Older smarty 生成静态化html文件