<pre class="brush:php">class dbstuff {
var $querynum = 0;
var $link;
var $charset;
var $sqls = array(); //自己添加,存储所有执行的sql语句 uican 2009-12-09
/**
* 连接数据库
*
* @param string $dbhost
* @param string $dbuser
* @param string $dbpw
* @param string $dbname
* @param int $pconnect 0为非持久连接 1为持久连接
* @param bool $halt
*/
function connect($dbhost,$dbuser,$dbpw,$dbname = '',$pconnect = 0,$halt = TRUE) {
if($pconnect) {
if(!$this->link = @mysql_pconnect($dbhost,$dbpw)) {
$halt && $this->halt('Can not connect to MySQL server');
}
} else {
if(!$this->link = @mysql_connect($dbhost,1)) {
$halt && $this->halt('Can not connect to MySQL server');
}
}
if($this->version() > '4.1') {
if($this->charset) {
@mysql_query("SET character_set_connection=$this->charset,character_set_results=$this->charset,character_set_client=binary",$this->link);
}
if($this->version() > '5.0.1') {
@mysql_query("SET sql_mode=''",$this->link);
}
}
//选择数据库
if($dbname) {
@mysql_select_db($dbname,$this->link);
}
}
/**
* 选择数据库
*
* @param string $dbname
* @return bool
*/
function select_db($dbname) {
return mysql_select_db($dbname,$this->link);
}
<pre class="brush:php">/**
- 获取查询的数组
-
- @param object $query
- @param string $result_type MYSQL_ASSOC 只得到关联索引,MYSQL_NUM 只得到数字索
- @return array
*/
function fetch_array($query,$result_type = MYSQL_ASSOC) {
return mysql_fetch_array($query,$result_type);
}
/**
-
执行一条sql语句
-
-
@param string $sql
-
@param string $type 如果$type为UNBUFFERED:则执行mysql_unbuffered_query();他与mysql_query的区别是,执行后不获取和缓存结果的行
-
@return object
/
function query($sql,$type = '') {
if(D_BUG) {
global $_SGLOBAL;
$sqlstarttime = $sqlendttime = 0;
$mtime = explode(' ',microtime());
$sqlstarttime = number_format(($mtime[1] + $mtime[0] - $_SGLOBAL['supe_starttime']),6) 1000;
}
$func = $type == 'UNBUFFERED' && @function_exists('mysql_unbuffered_query') ?
'mysql_unbuffered_query' : 'mysql_query';
if(!($query = $func($sql,$this->link)) && $type != 'SILENT') {
$this->halt('MySQL Query Error',$sql);
}
if(D_BUG) {
$mtime = explode(' ',microtime());
$sqlendttime = number_format(($mtime[1] + $mtime[0] - $_SGLOBAL['supe_starttime']),6) * 1000;
$sqltime = round(($sqlendttime - $sqlstarttime),3);
$explain = array();
$info = mysql_info();
if($query && preg_match("/^(select )/i",$sql)) {
$explain = mysql_fetch_assoc(mysql_query('EXPLAIN '.$sql,$this->link));
}
$_SGLOBAL['debug_query'][] = array('sql'=>$sql,'time'=>$sqltime,'info'=>$info,'explain'=>$explain);
}
$this->querynum++;
$this->sqls[] = $this->querynum.' '.$sql.' ';
return $query;
}
/**
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|