<?PHP [UCenter Home] (C) 2007-2008 Comsenz Inc.
$Id: class_MySQL.PHP 10484 2008-12-05 05:46:59Z liguode $
*/
if(!defined('IN_UCHOME')) {
exit('Access DenIEd');
}
class dbstuff {
var $querynum = 0;
var $link;
var $charset; 建立數據庫連接
@param string $dbhost
@param string $dbuser
@param string $dbpw
@param string $dbname
@param int $pconnect
@param bool $halt function connect($dbhost, $dbuser, $dbpw, $dbname = '', $pconnect = 0, $halt = TRUE) { if($pconnect) {
//mysql_pconnect() 函數打開一個到 MySQL 服務器的持久連接,跟我們常用的mysql_connect()不同,當腳本執行完畢後到 SQL 服務器的連接不會被關閉,此連接將保持打開以備以後使用(mysql_close() 不會關閉由 MySQL_pconnect() 建立的連接)
if(!$this->link = @MySQL_pconnect($dbhost, $dbuser, $dbpw)) {
$halt && $this->halt('Can not connect to MySQL Server'); } else {
if(!$this->link = @MySQL_connect($dbhost, $dbuser, $dbpw, 1)) { $halt && $this->halt('Can not connect to MySQL Server'); }
//獲取MySQL版本
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); } function select_db($dbname) {
return MySQL_select_db($dbname, $this->link); /** @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 要執行的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());
//sql開始時間與結束時間
$sqlstarttime = number_format(($mtime[1] + $mtime[0] - $_SGLOBAL['supe_starttime']), 6) * 1000; //如果定義$type='UNBUFFERED' 則執行MySQL_unbuffered_query()函數
$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); //將執行的query數加1
$this->querynum++;
return $query; //MySQL前一次操作所影響的行數
function affected_rows() {
return mysql_affected_rows($this->link); //MySQL錯誤提示
function error() {
return (($this->link) ? mysql_error($this->link) : mysql_error()); //返回上一個 MySQL 操作中的錯誤信息的數字編碼
function errno() {
return intval(($this->link) ? mysql_errno($this->link) : MySQL_errno()); //返回結果集中一個字段的值
function result($query, $row) {
$query = @MySQL_result($query, $row);
return $query; //返回結果集中行的數目
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); //返回上一步 INSERT 操作產生的 ID
function insert_id() {
return ($id = MySQL_insert_id($this->link)) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"), 0); //從結果集中取得一行作為數字數組
function fetch_row($query) {
$query = MySQL_fetch_row($query);
return $query; //從結果集中取得列信息
function fetch_fIElds($query) {
return mysql_fetch_fIEld($query); //獲取MySQL 服務器的版本號
function version() {
return MySQL_get_server_info($this->link); //關閉數據庫
function close() {
return mysql_close($this->link); //MySQL錯誤信息
function halt($message = '', $sql = '') { $dberror = $this->error(); $dberrno = $this->errno();
//rawurlencode();對 URL 進行編碼
$help_link = "http://faq.comsenz.com/?type=MySQL&dberrno=".rawurlencode($dberrno)."&dberror=".rawurlencode($dberror);
echo "<div style=\"position:absolute;font-size:11px;font-family:verdana,arial;background:#EBEBEB;padding:0.5em;\">
<b>MySQL Error</b><br>
<b>Message</b>: $message<br>
<b>SQL</b>: $sql<br>
<b>Error</b>: $dberror<br>
<b>Errno.</b>: $dberrno<br>
<a href=\"$help_link\" target=\"_blank\">Click here to seek help.</a>
</div>";
exit(); }
?>