封装对MySQL数据库操作的系列类(二)
<?
    $__TQUERY__
=True;
    
    if (!
$__TDATABASE__){
        include(
"$script_path/$dbtype/tdatabase.inc.php3");
    }
    
    class 
TQuery
    
{
        var 
$m_db//TDatabase object
        
var $m_result//the recordset
        
var $m_row//record values
        
        
function TQuery($db)
        {
            
$this->m_db=$db;
        }
        
        function 
query($SQL)
        {
//            echo $SQL;
            
$db=$this->m_db;
            unset(
$this->m_row);
            return 
$this->m_result=$db->query($SQL);
        }
        
        function 
fetch_array()
        {
            
$db=$this->m_db;
            
$this->m_row=$db->fetch_array($this->m_result);
            return 
$this->m_row;
        }
        
        function 
fetch_row()
        {
            
$db=$this->m_db;
            
$this->m_row=$db->fetch_row($this->m_result);
            return 
$this->m_row;            
        }
        
        function 
fetch_object()
        {
            
$db=$this->m_db;
            
$this->m_row=$db->fetch_object($this->m_result);
            return 
$this->m_row;            
        }
        
        function 
num_rows()
        {
            
$db=$this->m_db;
            return 
$db->num_rows($this->m_result);
        }
        
        function 
affected_rows()
        {
            
$db=$this->m_db;
            return 
$db->affected_rows($db->m_link);
        }
        
        function 
free_result()
        {
            
$db=$this->m_db;
            return 
$db->free_result($this->m_result);
        }
        
        function 
data_seek($offset)
        {
            return (
$this->num_rows())?mysql_data_seek($this->m_result,$offset):NULL;
        }
        
        function 
fetch_field($offset=-1)
        {
            if ((
$offset==-1) || (empty($offset))) {
                return 
mysql_fetch_field($this->m_result);
            }
            else{
                return 
mysql_fetch_field($this->m_result,$offset);
            }
        }
        
        function 
field_flags($offset)
        {
            return 
mysql_field_flags($this->m_result,$offset);
        }
    }
?>

//*****************************************
<?
    $__TSELECTQUERY__
=True;
    
    if (!
$__TQUERY__){
        include(
"$script_path/$dbtype/tquery.inc.php3");
    }
    if (!
$__TDATABASE__){
        include(
"$script_path/$dbtype/tdatabase.inc.php3");
    }
    
    class 
TSelectQuery extends TQuery
    
{
        var 
$m_offset//the current position in the recordset
        
var $m_num_rows=0//records number of the query
        
var $m_EOF//is at the end of recordset
        
var $m_BOF//is at the begining of the recordset
        
        
function TSelectQuery($db)
        {
            
$this->TQuery($db);
        }
        
        function 
query($SQL//do it!
        
{
            if (
eregi("^SELECT"trim($SQL))){ //must be a SELECT query
                
$db=$this->m_db;
                unset(
$this->m_row);
                
$this->m_result=$db->query($SQL);
                
$this->m_num_rows=$this->num_rows();
                
$this->m_offset=0;
                
$this->first();
                return 
$this->m_result;
            }
            else{
                return 
NULL;
            }
        }
        
        function 
EOF() //am i at the end of the recordset?
        
{
            return 
$this->m_EOF=($this->m_offset>=$this->m_num_rows);
        }
        
        function 
BOF() //am i at the begining of the recordset?
        
{
            return 
$this->m_BOF=($this->m_offset<0);
        }
        
        function 
first() //go to the first record
        
{
            
$this->m_offset=0;
            
$this->EOF();
            
$this->BOF();
            
$this->data_seek($this->m_offset);
        }
        
        function 
last() //go to the last record
        
{
            
$this->m_offset=$this->m_num_rows-1;
            
$this->EOF();
            
$this->BOF();
            
$this->data_seek($this->m_offset);
        }
        
        function 
next() //go to the next record
        
{
            ++
$this->m_offset;
            if (!
$this->EOF())
            {
                
$this->data_seek($this->m_offset);
            }
            
$this->BOF();
        }
        
        function 
prev() //go to the previous record
        
{
            --
$this->m_offset;
            if (!
$this->BOF())
            {
                
$this->data_seek($this->m_offset);
            }
            
$this->EOF();
        }
    }
?>