vsda2008 | 2010-07-20 13:13 |
dedecms连接多个mysql数据库的实现 dedecms连接多个数据库的实现方法实现实现二次开发教程
<?php if(!defined('DEDEINC')) { exit("Request Error!"); }
//调用这个类前,请先设定这些外部变量 /*---------------------------- $GLOBALS['cfg_dbhost']; $GLOBALS['cfg_dbuser']; $GLOBALS['cfg_dbpwd']; $GLOBALS['cfg_dbname']; $GLOBALS['cfg_dbprefix']; ----------------------------*/
$T960 = $t9 = new T960DedeSql(false); class T960DedeSql { var $linkID; var $dbHost; var $dbUser; var $dbPwd; var $dbName; var $dbPrefix; var $result; var $queryString; var $parameters; var $isClose; var $safeCheck;
//用外部定义的变量初始类,并连接数据库 function __construct($pconnect=false,$nconnect=true) { $this->isClose = false; $this->safeCheck = true; if($nconnect) { $this->Init($pconnect); } }
function T960DedeSql($pconnect=false,$nconnect=true) { $this->__construct($pconnect,$nconnect); }
function Init($pconnect=false) { $this->linkID = 0; $this->queryString = ''; $this->parameters = Array(); $this->dbHost = $GLOBALS['t960_cfg_dbhost']; $this->dbUser = $GLOBALS['t960_cfg_dbuser']; $this->dbPwd = $GLOBALS['t960_cfg_dbpwd']; $this->dbName = $GLOBALS['t960_cfg_dbname']; $this->dbPrefix = $GLOBALS['t960_cfg_dbprefix']; $this->result["me"] = 0; $this->Open($pconnect); }
function SetQuery($sql) { $prefix="dede_"; $sql = str_replace($prefix,$this->dbPrefix,$sql); $this->queryString = $sql; } function CopySQLPoint(&$ndsql) { $GLOBALS['T960'] = $ndsql; } function GetVersion($isformat=true) { global $T960; if($T960->isClose) { $this->Open(false); $T960->isClose = false; } $rs = mysql_query("SELECT VERSION();",$this->linkID); $row = mysql_fetch_array($rs); $mysql_version = $row[0]; mysql_free_result($rs); if($isformat) { $mysql_versions = explode(".",trim($mysql_version)); $mysql_version = number_format($mysql_versions[0].".".$mysql_versions[1],2); } return $mysql_version; } function Open($pconnect=false) { global $T960;
if($T960 && !$T960->isClose) { $this->linkID = $T960->linkID; } else { if(!$pconnect) { $this->linkID = @mysql_connect($this->dbHost,$this->dbUser,$this->dbPwd); } else { $this->linkID = @mysql_pconnect($this->dbHost,$this->dbUser,$this->dbPwd); } $GLOBALS['T960'] = $this; //CopySQLPoint($this); }
if(!$this->linkID) { $this->DisplayError("DedeCms错误警告:<font color='red'>连接数据库失败,可能数据库密码不对或数据库服务器出错!</font>"); exit(); } @mysql_select_db($this->dbName); $mysqlver = explode('.',$this->GetVersion()); $mysqlver = $mysqlver[0].'.'.$mysqlver[1]; if($mysqlver>4.0) { @mysql_query("SET NAMES '".$GLOBALS['cfg_db_language']."', character_set_client=binary, sql_mode='', interactive_timeout=3600 ;", $this->linkID); } return true; }
function SetSource($host,$username,$pwd,$dbname,$dbprefix="dede_") { $this->dbHost = $host; $this->dbUser = $username; $this->dbPwd = $pwd; $this->dbName = $dbname; $this->dbPrefix = $dbprefix; $this->result["me"] = 0; } function SelectDB($dbname) { mysql_select_db($dbname); }
function SetParameter($key,$value) { $this->parameters[$key]=$value; }
function SetLongLink() { @mysql_query("SET interactive_timeout=3600, wait_timeout=3600 ;", $this->linkID); }
function GetError() { $str = mysql_error(); return $str; }
function Close($isok=false) { $this->FreeResultAll(); if($isok) { mysql_close($this->linkID); $this->isClose = true; $GLOBALS['T960'] = null; } }
function ClearErrLink() { }
function CloseLink($dblink) { @mysql_close($dblink); } function ExecNoneQuery($sql='') { return $this->ExecuteNoneQuery($sql); }
function Execute($id="me", $sql='') { global $T960; if($T960->isClose) { $this->Open(false); $T960->isClose = false; } if(!empty($sql)) { $this->SetQuery($sql); }
if($this->safeCheck) { CheckSql($this->queryString); } $t1 = ExecTime(); $this->result[$id] = mysql_query($this->queryString,$this->linkID);
if($this->result[$id]===false) { $this->DisplayError(mysql_error()." <br />Error sql: <font color='red'>".$this->queryString."</font>"); } }
function Query($id="me",$sql='') { $this->Execute($id,$sql); }
function GetOne($sql='',$acctype=MYSQL_ASSOC) { global $T960; if($T960->isClose) { $this->Open(false); $T960->isClose = false; } if(!empty($sql)) { if(!eregi("limit",$sql)) $this->SetQuery(eregi_replace("[,;]$",'',trim($sql))." limit 0,1;"); else $this->SetQuery($sql); } $this->Execute("one"); $arr = $this->GetArray("one",$acctype); if(!is_array($arr)) { return ''; } else { @mysql_free_result($this->result["one"]); return($arr); } }
function ExecuteNoneQuery($sql='') { global $T960; if($T960->isClose) { $this->Open(false); $T960->isClose = false; } if(!empty($sql)) { $this->SetQuery($sql); } if(is_array($this->parameters)) { foreach($this->parameters as $key=>$value) { $this->queryString = str_replace("@".$key,"'$value'",$this->queryString); } }
if($this->safeCheck) CheckSql($this->queryString,'update'); return mysql_query($this->queryString,$this->linkID); }
function GetArray($id="me",$acctype=MYSQL_ASSOC) { if($this->result[$id]==0) { return false; } else { return mysql_fetch_array($this->result[$id],$acctype); } }
function GetObject($id="me") { if($this->result[$id]==0) { return false; } else { return mysql_fetch_object($this->result[$id]); } }
function IsTable($tbname) { $this->result[0] = mysql_list_tables($this->dbName,$this->linkID); while ($row = mysql_fetch_array($this->result[0])) { if(strtolower($row[0])==strtolower($tbname)) { mysql_freeresult($this->result[0]); return true; } } mysql_freeresult($this->result[0]); return false; }
}
?> |
|