/* *数据库操作类 */ class db_mysql { var $tablepre; var $version = ''; var $querynum = 0; var $curlink; var $link = array(); var $config = array(); var $sqldebug = array(); var $map = array(); /*构造函数,读入数据库相关配置*/ function db_mysql($config = array()) { if(!empty($config)) { $this->set_config($config); } } /* *设置数据库配置信息,设置表前缀 *不理解,这个$config里面的数据是怎么来的,也没见有赋值呀? *配置文件里的数据库配置信息是在$_config这个数组里存的, */ function set_config($config) { $this->config = &$config; $this->tablepre = $config['1']['tablepre']; if(!empty($this->config['map'])) { $this->map = $this->config['map']; } } /*连接数据库*/ function connect($serverid = 1) { if(empty($this->config) || empty($this->config[$serverid])) { $this->halt('config_db_not_found');//配置文件出错,则输出错误信息 } $this->link[$serverid] = $this->_dbconnect( $this->config[$serverid]['dbhost'], $this->config[$serverid]['dbuser'], $this->config[$serverid]['dbpw'], $this->config[$serverid]['dbcharset'], $this->config[$serverid]['dbname'], $this->config[$serverid]['pconnect'] ); $this->curlink = $this->link[$serverid]; } /*数据库连接验证!*/ function _dbconnect($dbhost, $dbuser, $dbpw, $dbcharset, $dbname, $pconnect) { $link = null; $func = empty($pconnect) ? 'mysql_connect' : 'mysql_pconnect'; if(!$link = @$func($dbhost, $dbuser, $dbpw, 1)) { $this->halt('notconnect'); } else { $this->curlink = $link; if($this->version() > '4.1') {//版本检测 $dbcharset = $dbcharset ? $dbcharset : $this->config[1]['dbcharset']; $serverset = $dbcharset ? 'character_set_connection='.$dbcharset.', character_set_results='.$dbcharset.', character_set_client=binary' : ''; $serverset .= $this->version() > '5.0.1' ? ((empty($serverset) ? '' : ',').'sql_mode=\'\'') : ''; $serverset && mysql_query("SET $serverset", $link); } $dbname && @mysql_select_db($dbname, $link); } return $link; } /*获取表名,暂时不太清楚map里存的是什么东西。*/ function table_name($tablename) { if(!empty($this->map) && !empty($this->map[$tablename])) { $id = $this->map[$tablename]; if(!$this->link[$id]) { $this->connect($id); } $this->curlink = $this->link[$id]; } else { $this->curlink = $this->link[1]; } return $this->tablepre.$tablename; } /*选择数据库*/ function select_db($dbname) { return mysql_select_db($dbname, $this->curlink); } /*将查询的结果集,转换为数组*/ function fetch_array($query, $result_type = MYSQL_ASSOC) { return mysql_fetch_array($query, $result_type); } /*根据名字推测是返回结果集中的第一条记录,不过代码还没看懂*/ function fetch_first($sql) { return $this->fetch_array($this->query($sql)); } /*返回结果集中的第一行*/ function result_first($sql) { return $this->result($this->query($sql), 0); } /*执行查询语句*/ function query($sql, $type = '') { //检测是否开启debug模块 if(defined('DISCUZ_DEBUG') && DISCUZ_DEBUG) { $starttime = dmicrotime(); } //判断查询类型是使用mysql_unbuffrerd_query,还是mysql_query //二者区别:http://www.w3school.com.cn/php/func_mysql_unbuffered_query.asp $func = $type == 'UNBUFFERED' && @function_exists('mysql_unbuffered_query') ? 'mysql_unbuffered_query' : 'mysql_query'; if(!($query = $func($sql, $this->curlink))) { if(in_array($this->errno(), array(2006, 2013)) && substr($type, 0, 5) != 'RETRY') { $this->connect(); return $this->query($sql, 'RETRY'.$type); } if($type != 'SILENT' && substr($type, 5) != 'SILENT') { $this->halt('query_error', $sql); } } if(defined('DISCUZ_DEBUG') && DISCUZ_DEBUG) { $this->sqldebug[] = array($sql, number_format((dmicrotime() - $starttime), 6), debug_backtrace()); } $this->querynum++;//查询次数 return $query; } /*返回update,insert,delete操作所影响的行数,执行成功则返回所影响的行数,否则返回-1*/ function affected_rows() { return mysql_affected_rows($this->curlink); } /*输出错误信息*/ function error() { return (($this->curlink) ? mysql_error($this->curlink) : mysql_error()); } /*返回上一次操作的错误编号,不带错误文本,如果没有错误则返回0*/ function errno() { return intval(($this->curlink) ? mysql_errno($this->curlink) : mysql_errno()); } /*返回结果集,可以指定返回某一行*/ function result($query, $row = 0) { $query = @mysql_result($query, $row); return $query; } /*返回select查询所影响的行数,只对selcet有效,若检测update,insert,delete,请使用mysql_affected_rows()*/ function num_rows($query) { $query = mysql_num_rows($query); return $query; } /*返回结果集中的字段数*/ function num_fields($query) { return mysql_num_fields($query); } /*释放资源所占内存*/ function free_result($query) { return mysql_free_result($query); } /*返回新插入行的自增id*/ function insert_id() { return ($id = mysql_insert_id($this->curlink)) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"), 0); } /*把返回的数据集生成数组,如果查询的数据集为空,则返回false*/ function fetch_row($query) { $query = mysql_fetch_row($query); return $query; } /*返回数据集中的字段信息,比如类型,长度等*/ function fetch_fields($query) { return mysql_fetch_field($query); } /*返回服务器上的mysql版本,返回一字符串*/ function version() { if(empty($this->version)) { $this->version = mysql_get_server_info($this->curlink); } return $this->version; } /*关闭连接*/ function close() { return mysql_close($this->curlink); } /*输入出相关错误提示,文件所在位置:source/class/class_error.php*/ function halt($message = '', $sql = '') { require_once libfile('class/error'); discuz_error::db_error($message, $sql); } }