MYSQL类库
作者: 天行
<?php
class DB
{
var $connection;
var $query_id = 0;
function DB( $db_host, $db_user, $db_password = "", $db_name = "", $db_pconnect = 1 )
{
$connect_handle = ( $db_pconnect ) ? "mysql_pconnect" : "mysql_connect";
if ( !$this->connection = $connect_handle( $db_host, $db_user, $db_password ) )
$this->error( "Could not connect to the database server ($db_host, $db_user).", 1 );
if ( [email protected]mysql_select_db( $db_name ) )
{
@mysql_close( $this->connection );
$this->error( "Could not select database ($db_name).", 1 );
}
return $this->connection;
}
function close()
{
if ( $this->connection )
{
if ( $this->query_id )
@mysql_free_result( $this->query_id );
return @mysql_close( $this->connection );
}
}
function query( $query = "" )
{
unset( $this->query_id );
if ( !$this->query_id = @mysql_query( $query, $this->connection ) )
$this->error( "<b>Bad SQL Query</b>: " . htmlentities( $query ) . "<br><b>" . mysql_error() . "</b>" );
return $this->query_id;
}
function queryArray( $query_id = -1, $assoc = 0 )
{
if ( $query_id != -1 )
$this->query_id = $query_id;
if ( $this->query_id )
return ( $assoc ) ? mysql_fetch_assoc( $this->query_id ) : mysql_fetch_array( $this->query_id );
}
function freeResult( $query_id = -1 )
{
if ( $query_id != -1 )
$this->query_id = $query_id;
return @mysql_free_result( $this->query_id );
}
function queryFirstRow( $query = "" )
{
$this->query( $query );
$result = $this->queryArray( $this->query_id );
$this->freeResult();
return $result;
}
function _GetNumRows( $query_id = -1 )
{
if ( $query_id != -1 )
$this->query_id = $query_id;
return @mysql_num_rows( $this->query_id );
}
function _GetNumFields( $query_id )
{
return @mysql_num_fields( $this->query_id );
}
function _GetInsertID()
{
return ( $this->connection ) ? @mysql_insert_id( $this->connection ) : 0;
}
function error( $errmsg = '' )
{
global $Config,$TEMPLATE,$lang;
if ( $Config['SQLerrorIsDisplay'] )
_ShowError($lang['MySQL_error'],$errmsg);
if( $Config['SQLerrorIsSend'] )
_SendMail($Config['SQLerrorSendMail'],$lang['MySQL_error'],$errmsg);
}
}
?>
class DB
{
var $connection;
var $query_id = 0;
function DB( $db_host, $db_user, $db_password = "", $db_name = "", $db_pconnect = 1 )
{
$connect_handle = ( $db_pconnect ) ? "mysql_pconnect" : "mysql_connect";
if ( !$this->connection = $connect_handle( $db_host, $db_user, $db_password ) )
$this->error( "Could not connect to the database server ($db_host, $db_user).", 1 );
if ( [email protected]mysql_select_db( $db_name ) )
{
@mysql_close( $this->connection );
$this->error( "Could not select database ($db_name).", 1 );
}
return $this->connection;
}
function close()
{
if ( $this->connection )
{
if ( $this->query_id )
@mysql_free_result( $this->query_id );
return @mysql_close( $this->connection );
}
}
function query( $query = "" )
{
unset( $this->query_id );
if ( !$this->query_id = @mysql_query( $query, $this->connection ) )
$this->error( "<b>Bad SQL Query</b>: " . htmlentities( $query ) . "<br><b>" . mysql_error() . "</b>" );
return $this->query_id;
}
function queryArray( $query_id = -1, $assoc = 0 )
{
if ( $query_id != -1 )
$this->query_id = $query_id;
if ( $this->query_id )
return ( $assoc ) ? mysql_fetch_assoc( $this->query_id ) : mysql_fetch_array( $this->query_id );
}
function freeResult( $query_id = -1 )
{
if ( $query_id != -1 )
$this->query_id = $query_id;
return @mysql_free_result( $this->query_id );
}
function queryFirstRow( $query = "" )
{
$this->query( $query );
$result = $this->queryArray( $this->query_id );
$this->freeResult();
return $result;
}
function _GetNumRows( $query_id = -1 )
{
if ( $query_id != -1 )
$this->query_id = $query_id;
return @mysql_num_rows( $this->query_id );
}
function _GetNumFields( $query_id )
{
return @mysql_num_fields( $this->query_id );
}
function _GetInsertID()
{
return ( $this->connection ) ? @mysql_insert_id( $this->connection ) : 0;
}
function error( $errmsg = '' )
{
global $Config,$TEMPLATE,$lang;
if ( $Config['SQLerrorIsDisplay'] )
_ShowError($lang['MySQL_error'],$errmsg);
if( $Config['SQLerrorIsSend'] )
_SendMail($Config['SQLerrorSendMail'],$lang['MySQL_error'],$errmsg);
}
}
?>