class
dbstuff {
var $querynum = 0 ;
var $link ;
var $charset ;
function connect( $dbhost , $dbuser , $dbpw , $dbname = '' , $pconnect = 0 , $halt = TRUE ){
if ( $pconnect ) {
// 打开一个mysql的持续连接
if ( ! $this -> link = @ mysql_pconnect ( $dbhost , $dbuser , $dbpw )) {
// 连接失败输出错误
$halt && $this -> halt( ' Can not connect to MySQL server ' );
}
} else {
// 使用mysql_connect连接数据库
if ( ! $this -> link = @ mysql_connect ( $dbhost , $dbuser , $dbpw )) {
$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);
}
}
// 选择 MySQL 数据库
function select_db( $dbname ) {
return mysql_select_db ( $dbname , $this -> link);
}
// 从结果集中取得一行作为关联数组,或数字数组
function fetch_array( $query , $result_type = MYSQL_ASSOC) {
return mysql_fetch_array ( $query , $result_type );
}
// 发送一条 MySQL 查询,返回结果
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 ++ ;
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 );
}
// 获得最新插入数据的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 );
}
// 检查数据库版本
function version() {
return mysql_get_server_info ( $this -> link);
}
// 关闭数据库连接
function close() {
return mysql_close ( $this -> link);
}
}
var $querynum = 0 ;
var $link ;
var $charset ;
function connect( $dbhost , $dbuser , $dbpw , $dbname = '' , $pconnect = 0 , $halt = TRUE ){
if ( $pconnect ) {
// 打开一个mysql的持续连接
if ( ! $this -> link = @ mysql_pconnect ( $dbhost , $dbuser , $dbpw )) {
// 连接失败输出错误
$halt && $this -> halt( ' Can not connect to MySQL server ' );
}
} else {
// 使用mysql_connect连接数据库
if ( ! $this -> link = @ mysql_connect ( $dbhost , $dbuser , $dbpw )) {
$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);
}
}
// 选择 MySQL 数据库
function select_db( $dbname ) {
return mysql_select_db ( $dbname , $this -> link);
}
// 从结果集中取得一行作为关联数组,或数字数组
function fetch_array( $query , $result_type = MYSQL_ASSOC) {
return mysql_fetch_array ( $query , $result_type );
}
// 发送一条 MySQL 查询,返回结果
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 ++ ;
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 );
}
// 获得最新插入数据的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 );
}
// 检查数据库版本
function version() {
return mysql_get_server_info ( $this -> link);
}
// 关闭数据库连接
function close() {
return mysql_close ( $this -> link);
}
}