Discuz 7 MYSQL 数据库操作类 分析

file:dz7 /include/db_mysql.class.php

[php]<?php

/*
[Discuz!] (C)2001-2009 Comsenz Inc.
This is NOT a freeware, use is subject to license terms

$Id: db_mysql.class.php 20294 2009-09-23 06:00:37Z zhaoxiongfei $
*/

if(!defined('IN_DISCUZ')) {
exit('Access Denied');
}
//检查是否是非法调用
class dbstuff {

var $version = '';
var $querynum = 0;
var $link = null;

function connect($dbhost, $dbuser, $dbpw, $dbname = '', $pconnect = 0, $halt = TRUE, $dbcharset2 = '') {

$func = empty($pconnect) ? 'mysql_connect' : 'mysql_pconnect';
if(!$this->link = @$func($dbhost, $dbuser, $dbpw, 1)) {
$halt && $this->halt('Can not connect to MySQL server');
} else {
if($this->version() > '4.1') {
global $charset, $dbcharset;
$dbcharset = $dbcharset2 ? $dbcharset2 : $dbcharset;
$dbcharset = !$dbcharset && in_array(strtolower($charset), array('gbk', 'big5', 'utf-8')) ? str_replace('-', '', $charset) : $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", $this->link);
}
$dbname && @mysql_select_db($dbname, $this->link);
}

}

function select_db($dbname) {
return mysql_select_db($dbname, $this->link);
}
//函数select_db($dbname)
//选择数据库,调用自带的mysql_select_db
function fetch_array($query, $result_type = MYSQL_ASSOC) {
return mysql_fetch_array($query, $result_type);
}
//函数fetch_array($query, $result_type = MYSQL_ASSOC)
//获取结果集的一条记录以数组形式返回

function fetch_first($sql) {
return $this->fetch_array($this->query($sql));
}
//函数fetch_first($sql)
//获取第一条记录,实际上就是fetcharray的数组里面取出的
function result_first($sql) {
return $this->result($this->query($sql), 0);
}
//函数result_first($sql) 
//只取出一个值,用了mysql的result

function query($sql, $type = '') {

global $debug, $discuz_starttime, $sqldebug, $sqlspenttimes;

if(defined('SYS_DEBUG') && SYS_DEBUG) {
@include_once DISCUZ_ROOT.'./include/debug.func.php';
sqldebug($sql);
}

$func = $type == 'UNBUFFERED' && @function_exists('mysql_unbuffered_query') ?
'mysql_unbuffered_query' : 'mysql_query';
if(!($query = $func($sql, $this->link))) {
if(in_array($this->errno(), array(2006, 2013)) && substr($type, 0, 5) != 'RETRY') {
$this->close();
require DISCUZ_ROOT.'./config.inc.php';
$this->connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect, true, $dbcharset);
return $this->query($sql, 'RETRY'.$type);
} elseif($type != 'SILENT' && substr($type, 5) != 'SILENT') {
$this->halt('MySQL Query Error', $sql);
}
}

$this->querynum++;
return $query;
}
//函数query($sql, $type = '') 
//这个不用说,执行查询直接
function affected_rows() {
return mysql_affected_rows($this->link);
}
//返回影响条数

function error() {
return (($this->link) ? mysql_error($this->link) : mysql_error());
}
//返回错误信息

function errno() {
return intval(($this->link) ? mysql_errno($this->link) : mysql_errno());
}
//返回错误编号,mysql错误编号

function result($query, $row = 0) {
$query = @mysql_result($query, $row);
return $query;
}
//mysql_result 只取一个,屏蔽了错误提示

function num_rows($query) {
$query = mysql_num_rows($query);
return $query;
}
//结果条数,$query规定要使用的数据指针。该数据指针是 mysql_query() 函数产生的结果

function num_fields($query) {
return mysql_num_fields($query);
}
//返回结果集中字段总数,$query规定要使用的数据指针。该数据指针是 mysql_query() 函数产生的结果

function free_result($query) {
return mysql_free_result($query);
}
//释放结果集,仅需要在考虑到返回很大的结果集时会占用多少内存时调用。在脚本结束后所有关联的内存都会被自动释放。

function insert_id() {
return ($id = mysql_insert_id($this->link)) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"), 0);
}
//返回自增ID,实际上就是某些ID采用autoincrement,这个方法可以取回最后一个自增的ID是多少

function fetch_row($query) {
$query = mysql_fetch_row($query);
return $query;
}
//从结果集中取得一行作为枚举数组

function fetch_fields($query) {
return mysql_fetch_field($query);
}
//从结果集中取得列信息并作为对象返回

function version() {
if(empty($this->version)) {
$this->version = mysql_get_server_info($this->link);
}
return $this->version;
}
//显示mysql版本信息

function close() {
return mysql_close($this->link);
}
//关闭MySQL连接

function halt($message = '', $sql = '') {
define('CACHE_FORBIDDEN', TRUE);
require_once DISCUZ_ROOT.'./include/db_mysql_error.inc.php';
}
}
//输出错误信息

?>
By ihipop.gicp.net

[/php]

Author Info :
  • From:Discuz 7 MYSQL 数据库操作类 分析
  • URL:https://blog.ihipop.info/2010/04/1139.html
  • Please Reserve This Link,Thanks!
  • 发表评论

    您的电子邮箱地址不会被公开。 必填项已用*标注