CSDN博客

img bjbs_270

MySQL类

发表于2004/9/28 11:32:00  1340人阅读

分类: PHP--Class

PHP代码:

<?php

/**
* @ Page : Class.MySQL.PHP
* @ Copyright : Snakevil, 1998-2004
* @ Author : Snakevil
*
* @ Usage :
* 定义 MySQL 数据库操作类。
*
* @ Notes :
*  + 3:30 2004-6-6
*    正式完成最初版本。
*/

/**- Content Start -----------------------------------------------------------*/

if (defined('SWORK_COPYRIGHT') && SWORK_COPYRIGHT === 'SNakeVil 1998-2004') {
/** 类封装开始 */

/**
* @ Purpose :
* _swork_ 标准 database 类 MySQL
* @ Package Name : MySQL
* @ Author : SNakeVil
* @ Modifications :
*
* @ See Alse :
*/
class FwSnvMySQL extends FwSnvBaseClass {
    
/**
     * @ Purpose :
     * 服务器地址
     * @ Attribute Name : mHost
     * @ Type : string
     */
    
var $mHost;
    
/**
     * @ Purpose :
     * 数据库用户
     * @ Attribute Name : mUser
     * @ Type : string
     */
    
var $mUser;
    
/**
     * @ Purpose :
     * 数据库密码
     * @ Attribute Name : mPassword
     * @ Type : string
     */
    
var $mPassword;
    
/**
     * @ Purpose :
     * 数据库名称
     * @ Attribute Name : mDbName
     * @ Type : string
     */
    
var $mDbName;
    
/**
     * @ Purpose :
     * 数据库表前缀
     * @ Attribute Name : mTblPrefix
     * @ Type : string
     */
    
var $mTblPrefix;
    
/**
     * @ Purpose :
     * 连接资源编号
     * @ Attribute Name : mLinkId
     * @ Type : resource
     */
    
var $mLinkId;
    
/**
     * @ Purpose :
     * 处理资源编号
     * @ Attribute Name : mQueryId
     * @ Type : resource
     */
    
var $mQueryId;
    
/**
     * @ Purpose :
     * 引用检索资源
     * @ Attribute Name : Results
     * @ Type : reference FwSnvMySQLRSet
     */
    
var $Results;
    
/**
     * @ Purpose :
     * 检索结果是否索引
     * @ Attribute Name : mIsBufferd
     * @ Type : boolean
     */
    
var $mIsBufferd;
    
/**
     * @ Purpose :
     * 保存最后执行的 SQL 语句
     * @ Attribute Name : mLastSQL
     * @ Type : string
     */
    
var $mLastSQL;
    
/**
     * @ Purpose :
     * 最后 SQL 语句影响的数据行数
     * @ Attribute Name : mLastAffectd
     * @ Type : integer
     */
    
var $mLastAffectd;
    
/**
     * @ Purpose :
     * 最后 SQL::INSERT 语句返回的编号
     * @ Attribute Name : mLastInsertd
     * @ Type : integer
     */
    
var $mLastInsertd;

    
/**
     * @ Initialize Method @
     * @ Parameters :
     * string $_Host 服务器地址
     * string $_User 数据库用户
     * string $_Password 数据库密码
     * string $_DbName 数据库名称
     * string $_TblPrefix 数据库表前缀
     */
    
function FwSnvMySQL($_Host = '', $_User = '', $_Password = '', $_DbName = '', $_TblPrefix = '') {
        
parent::FwSnvBaseClass();
        
$this->mLinkId = 0;
        
$this->mQueryId = 0;
        
$this->mLastTblName = '';
        
$this->Results = NULL;
        
$this->mIsBufferd = TRUE;
        
$this->mLastSQL = '';
        
$this->mLastAffectd = -1;
        
$this->mLastInsertd = -1;
        
$this->mHost = (is_string($_Host)) ? $_Host : '';
        
$this->mUser = (is_string($_User)) ? $_User : '';
        
$this->mPassword = (is_string($_Password)) ? $_Password : '';
        
$this->mDbName = (is_string($_DbName)) ? $_DbName : '';
        
$this->mTblPrefix = (is_string($_TblPrefix)) ? $_TblPrefix : '';
        if (
$this->mHost != '') $this->Connect();
    }

    
/**
     * @ Purpose :
     * 连接数据库服务器。
     * @ Method Name : Connect()
     * @ Parameters :
     * string $_Host 服务器地址
     * string $_User 数据库用户
     * string $_Password 数据库密码
     * string $_DbName 数据库名称
     * string $_TblPrefix 数据库表前缀
     * @ Return : boolean 连接是否成功
     */
    
function Connect($_Host = '', $_User = '', $_Password = '', $_DbName = '', $_TblPrefix = '') {
        if (
$this->IsError()) return FALSE;
        
/** 如果已连接,相同连接参数则保持,否则断开原有连接重连 */
        
if (is_resource($this->mLinkId)) {
            if (
$this->mHost == $_Host) return TRUE;
            else {
                
$this->Close();
            }
        }
        
/** 将与参数值不等的原有属性值覆盖 */
        
if (is_string($_Host)
            &&
$_Host != $this->mHost
            
&& $_Host != '')
            
$this->mHost = $_Host;
        if (
is_string($_User)
            &&
$_User != $this->mUser &&
            
$_User != '')
            
$this->mUser = $_User;
        if (
is_string($_Password)
            &&
$_Password != $this->mPassword
            
&& $_Password != '')
            
$this->mPassword = $_Password;
        if (
is_string($_DbName)
            &&
$_DbName != $this->mDbName
            
&& $_DbName != '')
            
$this->mDbName = $_DbName;
        if (
is_string($_TblPrefix)
            &&
$_TblPrefix != $this->mTblPrefix
            
&& $_TblPrefix != '')
            
$this->mTblPrefix = $_TblPrefix;
        if (
$this->mHost != '') {
            
$this->mLinkId = @mysql_connect($this->mHost, $this->mUser, $this->Password);
            if (
is_resource($this->mLinkId)) {
                if (
$this->mDbName != '')
                    
$this->SelectDb();
                return
TRUE;
            }
        }
        
/** 连接失败 */
        
$this->mLinkId = 0;
        return
$this->Error(2);
    }

    
/**
     * @ Purpose :
     * 选择数据库。
     * @ Method Name : SelectDb()
     * @ Parameters :
     * string $_DbName 数据库名称
     * @ Return : boolean 选择是否成功
     */
    
function SelectDb($_DbName = '') {
        if (
$this->IsError()) return FALSE;
        if (
is_string($_DbName) && $_DbName != '') $this->mDbName = $_DbName;
        
/** 尚未连接数据库时强制连接数据库 */
        
if (!is_resource($this->mLinkId)) return $this->Connect();
        if (@
mysql_select_db($this->mDbName, $this->mLinkId))
            return
TRUE;
        else {
            
/** 无法选择相应的数据库,返回错误 */
            
return $this->Error(3);
        }
    }

    
/**
     * @ Purpose :
     * 设置检索结果是否缓存
     * @ Method Name : SetBufferType()
     * @ Parameters :
     * boolean $_IsBufferd 是否缓存结果
     * @ Return : boolean 设置成功
     */
    
function SetBufferType($_IsBufferd = TRUE) {
        if (
$this->IsError()) return FALSE;
        if (
$_IsBufferd == 1) $this->mIsBufferd = TRUE;
        else if (
$_IsBufferd == 0) $this->mIsBufferd = FALSE;
        else
$this->mIsBufferd = TRUE;
        return
TRUE;
    }

    
/**
     * @ Purpose :
     * 执行 SQL 语句
     * @ Method Name : Execute()
     * @ Parameters :
     * string $_SQL 要执行的 SQL 语句
     * boolean $_IsBufferd 是否缓存结果
     * @ Return : boolean 是否执行成功
     */
    
function Execute($_SQL = '', $_IsBufferd = 8486) {
        if (
$this->IsError()) return FALSE;
        
/** 若尚未连接服务器,尝试连接 */
        
if (!is_resource($this->mLinkId) && FALSE === $this->Connect())
            return
FALSE;
        if (!
is_string($_SQL)) return FALSE;
        if (
$_IsBufferd != 1 && $_IsBufferd != 0)
            
$_IsBufferd = $this->mIsBufferd;
        
$command = ($_IsBufferd) ? 'mysql_query' : 'mysql_unbuffered_query';
        if (
$_IsBufferd) {
            
$this->mQueryId = @mysql_query($_SQL, $this->mLinkId);
            if (
FALSE === $this->mQueryId) {
                
/** 无法正确处理 SQL 语句,返回错误 */
                
return $this->Error(4);
            }
            
$this->mLastSQL = $_SQL;
            
$this->mLastAffectd = -1;
            
$this->mLastInsertd = -1;
            
$this->Results = NULL;
            
/** 取得当前 SQL 语句的关键字 */
            
$wrap_pos = strpos($_SQL, ' ');
            if (
FALSE === $wrap_pos) $key_word = $_SQL;
            else
$key_word = substr($_SQL, 0, $wrap_pos);
            
$key_word = strtolower($key_word);
            switch (
$key_word) {
                case
'insert':
                    
$this->mLastInsertd = mysql_insert_id($this->mLinkId);
                case
'delete':
                case
'replace':
                case
'update':
                    
$this->mLastAffectd = mysql_affected_rows($this->mLinkId);
                    break;
                case
'select':
                case
'show':
                case
'describe':
                case
'explain':
                    
$this->Results =& new FwSnvMySQLRSet($this->mLinkId, $this->mQueryId);
                default :
            }
        } else {
            if (
FALSE === @mysql_unbuffered_query($_SQL, $this->mLinkId)) {
                
/** 无法正确处理 SQL 语句,返回错误 */
                
return $this->Error(4);
            }
            
$this->mLastSQL = $_SQL;
            
$this->mLastAffectd = -1;
            
$this->mLastInsertd = -1;
            
$this->Results = NULL;
        }
        return
TRUE;
    }

/**- Methods Following : Trying to Simplify SQL Process ----------------------*/

    /**
     * @ Purpose :
     * 执行 SQL::INSERT 语句
     * @ Method Name : Insert()
     * @ Parameters :
     * boolean $_IsBufferd 是否缓存结果
     * mixed $_TblPrefix 表名称前缀
     * string $_TblName 表名称
     * mixed $_Fields 要添加的字段
     * mixed $_Values_N 要添加的第 N 份值
     * @ Return : boolean 是否执行成功
     * @ See Also :
     * 本方法参数量不定,顺序分别为 $_IsBufferd, $_TblPrefix, $_TblName,
     *   $_Fields,其后所有出现参数均为要添加的值
     */
    
function Insert() {
        if (
$this->IsError()) return FALSE;
        
$arg_count = func_num_args();
        if (
$arg_count < 5) return FALSE;
        
$func_args = func_get_args();
        
$_IsBufferd = $func_args[0];
        
/** 生成进行处理的表的名称 */
        
$_SQL = ($func_args[1] == SWORK_DEFAULT)
            ?
$this->mTblPrefix
            
: $func_args[1];
        
$_SQL = 'INSERT INTO `'.addslashes($_SQL.$func_args[2]).'` (`';
        
/** 获取要添加的字段列表 */
        
$_SQL .= (is_array($func_args[3]))
            ?
addslashes(implode('`, `', $func_args[3]))
            :
addslashes($func_args[3]);
        
$_SQL .= '`) VALUES ('';
        /** 对要添加的值进行处理 */
        $t_arr = array();
        for ($i = 0, $j = $arg_count - 4; $i < $j; $i++) {
            $t_arr[] = (is_array($func_args[$i]))
                ? str_replace('
-%sQv0te%-', ''', addslashes(implode('-%sQv0te%-, -%sQv0te%-', $func_args[$i])))
                :
addslashes($func_args[$i]);
        }
        
$_SQL .= implode(''), ('', $t_arr).'')';
        return $this->Execute($_SQL, $_IsBufferd);
    }

    /**
     * @ Purpose :
     * 执行 SQL:<img src="images/smilies/biggrin.gif" border="0" alt="">ELETE 语句
     * @ Method Name : Delete()
     * @ Parameters :
     * boolean $_IsBufferd 是否缓存结果
     * mixed $_TblPrefix 表名称前缀
     * string $_TblName 表名称
     * string $_WhereClause 检索条件
     * string $_LimitClause 限制处理的数据记录数
     * @ Return : boolean 是否执行成功
     */
    function Delete($_IsBufferd = 8486, $_TblPrefix = '', $_TblName = '', $_WhereClause = '', $_LimitClause = '') {
        if ($this->IsError()) return FALSE;
        $_SQL = ($_TblPrefix == SWORK_DEFAULT)
            ? $this->mTblPrefix
            : $_TblPrefix;
        $_SQL = '
DELETE FROM `'.addslashes($_SQL.$_TblName).'`';
        if ($_WhereClause != '') $_SQL .= '
WHERE '.$_WhereClause;
        if (is_int($_LimitClause)) $_SQL .= '
LIMIT '.$_LimitClause;
        return $this->Execute($_SQL, $_IsBufferd);
    }

    /**
     * @ Purpose :
     * 执行 SQL::REPLACE 语句
     * @ Method Name : Replace()
     * @ Parameters :
     * boolean $_IsBufferd 是否缓存结果
     * mixed $_TblPrefix 表名称前缀
     * string $_TblName 表名称
     * mixed $_Fields 要改变的字段
     * mixed $_Values 要改变的值
     * @ Return : boolean 是否执行成功
     */
    function Replace($_IsBufferd = 8486, $_TblPrefix = '', $_TblName = '', $_Fields = '', $_Values = '') {
        if ($this->IsError()) return FALSE;
        $_SQL = ($_TblPrefix == SWORK_DEFAULT)
            ? $this->mTblPrefix
            : $_TblPrefix;
        $_SQL = '
REPLACE INTO `'.addslashes($_SQL.$_TblName).'` (`';
        /** 获取要添加的字段列表 */
        $_SQL .= (is_array($_Fields))
            ? addslashes(implode('
`, `', $_Fields))
            : addslashes($_Fields);
        $_SQL .= '
`) VALUES ('';
        
/** 对要添加的值进行处理 */
        
$_SQL .= (is_array($_Values))
            ?
str_replace('-%sQv0te%-', ''', addslashes(implode('-%sQv0te%-, -%sQv0te%-', $_Values)))
            : addslashes($_Values);
        $_SQL .= '')'
;
        return
$this->Execute($_SQL, $_IsBufferd);
    }

    
/**
     * @ Purpose :
     * 执行 SQL::UPDATE 语句
     * @ Method Name : Update()
     * @ Parameters :
     * boolean $_IsBufferd 是否缓存结果
     * mixed $_TblPrefix 表名称前缀
     * string $_TblName 表名称
     * mixed $_Fields 要更新的字段
     * mixed $_Values 要更新的值
     * string $_WhereClause 检索条件
     * string $_LimitClause 限制处理的数据记录数
     * @ Return : boolean 是否执行成功
     */
    
function Update($_IsBufferd = 8486, $_TblPrefix = '', $_TblName = '', $_Fields = '', $_Values = '', $_WhereClause = '', $_LimitClause = '') {
        if (
$this->IsError()) return FALSE;
        
$_SQL = ($_TblPrefix == SWORK_DEFAULT)
            ?
$this->mTblPrefix
            
: $_TblPrefix;
        
$_SQL = 'UPDATE `'.addslashes($_SQL.$_TblName).'` SET ';
        if (!
is_array($_Fields)) $_Fields = array($_Fields);
        if (!
is_array($_Values)) $_Values = array($_Values);
        
$value_count = count($_Values);
        for (
$i = 0, $j = count($_Fields); $i < $j; $i++) {
            if (
$i) $_SQL .= ', ';
            
$_SQL .= '`'.addslashes($_Fields[$i]).'` = '';
            $_SQL .= ($i < $value_count)
                ? addslashes($_Values[$i])
                : '';
            $_SQL .= '''
;
        }
        if (
$_WhereClause != '') $_SQL .= ' WHERE '.$_WhereClause;
        if (
is_int($_LimitClause)) $_SQL .= ' LIMIT '.$_LimitClause;
        return
$this->Execute($_SQL, $_IsBufferd);
    }

    
/**
     * @ Purpose :
     * 执行 SQL::SELECT ... JOIN ... 中 JOIN 语句
     * @ Method Name : Join()
     * @ Parameters :
     * mixed $_TblPrefix 表名称前缀
     * string $_TblName 表名称
     * string $_AliasName 表别名
     * string $_WhereClause 匹配条件
     * string $_OtherJoin 其他 JOIN 语句
     * @ Return : mixed 生成的 SQL 语句
     */
    
function Join($_TblPrefix = '', $_TblName = '', $_AliasName = '', $_WhereClause = '', $_OtherJoin = '') {
        if (
$this->IsError()) return FALSE;
        
$_SQL = ($_TblPrefix == SWORK_DEFAULT)
            ?
$this->mTblPrefix
            
: $_TblPrefix;
        
$_SQL = 'LEFT JOIN `'.addslashes($_SQL.$_TblName).'` ';
        if (
$_AliasName != '') $_SQL .= 'AS `'.addslashes($_AliasName).'` ';
        
$_SQL .= 'ON '.$_WhereClause;
        if (
$_OtherJoin != '') $_SQL .= ' '.$_OtherJoin;
        return
$_SQL;
    }

    
/**
     * @ Purpose :
     * 执行 SQL::SELECT 语句
     * @ Method Name : Select()
     * @ Parameters :
     * boolean $_IsBufferd 是否缓存结果
     * mixed $_TblPrefix 表名称前缀
     * string $_TblName 表名称
     * string $_AliasName 表别名
     * mixed $_Fields 要检索的字段
     * string $_OtherJoin 其他 JOIN 语句
     * string $_WhereClause 匹配条件
     * string $_OrderByClause 排序方式
     * string $_GroupByClause 分组简化方式
     * string $_LimitClause 限制处理的数据记录数
     * string $_OffsetClause 限制处理的数据偏移数
     * @ Return : boolean 是否执行成功
     */
    
function Select($_IsBufferd = 8486, $_TblPrefix = '', $_TblName = '', $_AliasName = '', $_Fields = '', $_OtherJoin = '', $_WhereClause = '', $_LimitClause = '', $_OrderByClause = '', $_OffsetClause = '', $_GroupByClause = '') {
        if (
$this->IsError()) return FALSE;
        
$_SQL = ($_TblPrefix == SWORK_DEFAULT)
            ?
$this->mTblPrefix
            
: $_TblPrefix;
        
$_SQL = 'SELECT '.addslashes($_Fields).' FROM `'
            
.addslashes($_SQL.$_TblName).'`';
        if (
$_AliasName != '') $_SQL .= ' AS `'.addslashes($_AliasName).'`';
        if (
$_OtherJoin != '') $_SQL .= ' '.$_OtherJoin;
        if (
$_WhereClause != '') $_SQL .= ' WHERE '.$_WhereClause;
        if (
$_GroupByClause != '') $_SQL .= ' GROUP BY '.$_GroupByClause;
        if (
$_OrderByClause != '') $_SQL .= ' ORDER BY '.$_OrderByClause;
        if (
is_int($_LimitClause)) {
            
$_SQL .= ' LIMIT ';
            if (
is_int($_OffsetClause)) $_SQL .= $_OffsetClause.', ';
            
$_SQL .= $_LimitClause;
        }
        return
$this->Execute($_SQL, $_IsBufferd);
    }

/**- Methods Above : Trying to Simplify SQL Process --------------------------*/

    /**
     * @ Purpose :
     * 获取 SQL::INSERT 语句得到的编号。
     * @ Method Name : GetInserted()
     * @ Return : mixed 返回编号
     */
    
function GetInserted() {
        if (
$this->IsError()) return FALSE;
        if (
$this->mLastInsertd == -1) return FALSE;
        return
$this->mLastInsertd;
    }

    
/**
     * @ Purpose :
     * 获取 SQL 语句影响的数据行数。
     * @ Method Name : GetAffected()
     * @ Return : mixed 返回行数
     */
    
function GetAffected() {
        if (
$this->IsError()) return FALSE;
        if (
$this->mLastAffectd == -1) return FALSE;
        return
$this->mLastAffectd;
    }

    
/**
     * @ Purpose :
     * 释放检索资源。
     * @ Method Name : FreeResult()
     * @ Return : boolean 释放成功
     */
    
function FreeResult() {
        if (
$this->IsError()) return FALSE;
        if (isset(
$this->Results)) {
            
$this->Results->Close();
            
$this->Results = NULL;
            
$this->mLastAffectd = -1;
            
$this->mLastInsertd = -1;
        }
        return
TRUE;
    }

    
/**
     * @ Purpose :
     * 断开数据库连接。
     * @ Method Name : Close()
     * @ Return : boolean 断开成功
     */
    
function Close() {
        if (
$this->IsError()) return FALSE;
        if (
is_resource($this->mLinkId)) {
            
/** 释放检索资源 */
            
$this->FreeResult();
            @
mysql_close($this->mLinkId);
            
$this->mLinkId = 0;
            
$this->mHost = '';
            
$this->mUser = '';
            
$this->mPassword = '';
            
$this->mDbName = '';
            
$this->mTblPrefix = '';
        }
        return
TRUE;
    }

}

/**
* @ Purpose :
* _swork_ 标准 database 类 MySQL 的数据集
* @ Package Name : MySQLRSet
* @ Author : SNakeVil
* @ Modifications :
*
* @ See Alse :
*/
class FwSnvMySQLRSet extends FwSnvBaseClass {
    
/**
     * @ Purpose :
     * 引用数据库连接
     * @ Attribute Name : mLinkId
     * @ Type : reference resource
     */
    
var $mLinkId;
    
/**
     * @ Purpose :
     * 引用数据库检索
     * @ Attribute Name : mQueryId
     * @ Type : reference resource
     */
    
var $mQueryId;
    
/**
     * @ Purpose :
     * 检索数据集行总数
     * @ Attribute Name : mRowTotal
     * @ Type : integer
     */
    
var $mRowTotal;
    
/**
     * @ Purpose :
     * 检索数据集字段总数
     * @ Attribute Name : mFieldTotal
     * @ Type : integer
     */
    
var $mFieldTotal;
    
/**
     * @ Purpose :
     * 检索模式
     * @ Attribute Name : mFetchMode
     * @ Type : constant
     */
    
var $mFetchMode;
    
/**
     * @ Purpose :
     * 字段数据集合
     * @ Attribute Name : mFields
     * @ Type : array
     */
    
var $mFields;
    
/**
     * @ Purpose :
     * 数据集指针
     * @ Attribute Name : mPointer
     * @ Type : integer
     */
    
var $mPointer;
    
/**
     * @ Purpose :
     * 当前数据行的数据集合
     * @ Attribute Name : mRowData
     * @ Type : array
     */
    
var $mRowData;

    
/**
     * @ Initialize Method @
     * @ Parameters :
     * reference resource $_LinkId 数据库连接
     * reference resource $_QueryId 数据库检索
     */
    
function FwSnvMySQLRSet(& $_LinkId, & $_QueryId) {
        
$this->mFields = array();
        if (
is_resource($_LinkId) && is_resource($_QueryId)) {
            
$this->mLinkId = $_LinkId;
            
$this->mQueryId = $_QueryId;
            
$this->mFieldTotal = @mysql_num_fields($this->mQueryId);
            
$this->mRowTotal = @mysql_num_rows($this->mQueryId);
            for (
$i = 0, $j = $this->mFieldTotal; $i < $j; $i++) {
                
$f_name = mysql_field_name($this->mQueryId, $i);
                
$f_type = mysql_field_type($this->mQueryId, $i);
                
$f_len = mysql_field_len($this->mQueryId, $i);
                
$f_f = mysql_field_flags($this->mQueryId, $i);
                
$f_table = mysql_field_table($this->mQueryId, $i);
                
$f_f_null = (int)(FALSE === strpos($f_f, 'not_null'));
                
$f_f_primary_key = (int)(FALSE !== strpos($f_f, 'primary_key'));
                
$f_f_unsigned = (int)(FALSE !== strpos($f_f, 'unsigned'));
                
$f_f_zerofill = (int)(FALSE !== strpos($f_f, 'zerofill'));
                
$f_f_binary = (int)(FALSE !== strpos($f_f, 'binary'));
                
$f_f_index = (int)(FALSE !== strpos($f_f, 'multiple_key'));
                
$f_f_unique = (int)(FALSE !== strpos($f_f, 'unique_key'));
                
$f_f_auto_inc = (int)(FALSE !== strpos($f_f, 'auto_increment'));
                
$f_array = array(
                    
$f_name,
                    
$f_type,
                    
$f_len,
                    
$f_table,
                    
$f_f_null,
                    
$f_f_primary_key,
                    
$f_f_index,
                    
$f_f_unique,
                    
$f_f_unsigned,
                    
$f_f_zerofill,
                    
$f_f_auto_inc,
                    
'name' => $f_name,
                    
'type' => $f_type,
                    
'max_length' => $f_len,
                    
'table_in' => $f_table,
                    
'null' => $f_f_null,
                    
'primary_key' => $f_f_primary_key,
                    
'multiple_key' => $f_f_index,
                    
'unique_key' => $f_f_unique,
                    
'unsigned' => $f_f_unsigned,
                    
'zerofill' => $f_f_zerofill,
                    
'binary' => $f_f_binary,
                    
'auto_increment' => $f_f_auto_inc
                
);
                
$this->mFields[$i] = $f_array;
                
$this->mFields[$f_name] = $f_array;
            }
            
$this->MoveToBegin();
        } else {
            
$this->Error(5);
            
$this->mLinkId = 0;
            
$this->mQueryId = 0;
            
$this->mFieldTotal = 0;
            
$this->mRowTotal = 0;
            
$this->mPointer = 0;
        }
        
$this->SetFetchMode();
    }

    
/**
     * @ Purpose :
     * 设置数据集采集模式。
     * @ Method Name : SetFetchMode()
     * @ Parameters :
     * constant $_FetchMode PHP 内置 MySQL 数据集采集模式
     * @ Return : boolean 设置成功
     */
    
function SetFetchMode($_FetchMode = MYSQL_BOTH) {
        if (
$this->IsError()) return FALSE;
        if (
$_FetchMode == MYSQL_ASSOC) $this->mFetchMode = MYSQL_ASSOC;
        else if (
$_FetchMode == MYSQL_NUM) $this->mFetchMode = MYSQL_NUM;
        else
$this->mFetchMode = MYSQL_BOTH;
        return
TRUE;
    }

    
/**
     * @ Purpose :
     * 获取行总数
     * @ Method Name : CountRows()
     * @ Return : integer 行总数
     */
    
function CountRows() {
        if (
$this->IsError()) return FALSE;
        return
$this->mRowTotal;
    }

    
/**
     * @ Purpose :
     * 获取字段总数
     * @ Method Name : CountFields()
     * @ Return : integer 字段总数
     */
    
function CountFields() {
        if (
$this->IsError()) return FALSE;
        return
$this->mFieldTotal;
    }

    
/**
     * @ Purpose :
     * 清除检索结果资源
     * @ Method Name : Close()
     * @ Return : boolean 清除成功
     */
    
function Close() {
        if (
$this->IsError()) return FALSE;
        @
mysql_free_result($this->mQueryId);
        return
TRUE;
    }

    
/**
     * @ Purpose :
     * 获取字段信息
     * @ Method Name : GetFieldInfo()
     * @ Parameters :
     * mixed $_Field 确定字段的编号或名称
     * constant $_FetchMode PHP 内置 MySQL 数据集采集模式
     * @ Return : mixed 字段信息
     */
    
function GetFieldInfo($_Field = '', $_FetchMode = 8486) {
        if (
$this->IsError()) return FALSE;
        if (!isset(
$this->mFields[$_Field])) return FALSE;
        if (
$_FetchMode != MYSQL_ASSOC
            
&& $_FetchMode != MYSQL_NUM
            
&& $_FetchMode != MYSQL_BOTH)
            
$_FetchMode = $this->mFetchMode;
        
$t_arr1 = $this->mFields[$_Field];
        
$t_arr2 = array();
        while (list(
$k, $v) = each($t_arr1)) {
            if (
is_int($k)) {
                if (
$_FetchMode != MYSQL_ASSOC) $t_arr2[$k] = $v;
            } else {
                if (
$_FetchMode != MYSQL_NUM) $t_arr2[$k] = $v;
            }
        }
        return
$t_arr2;
    }

    
/**
     * @ Purpose :
     * 设置指针位置。
     * @ Method Name : MoveTo()
     * @ Parameters :
     * integer $_Pointer 新指针位置
     * @ Return : boolean 设置是否成功
     */
    
function MoveTo($_Pointer = 1) {
        if (
$this->IsError()) return FALSE;
        if (!
is_int($_Pointer)) $_Pointer = intval($_Pointer);
        if (
$_Pointer < 1 || $_Pointer > $this->mRowTotal) $_Pointer = 1;
        if (!
$this->mRowTotal) {
            
$this->mPointer = 0;
            return
FALSE;
        } else if (!@
mysql_data_seek($this->mQueryId, $_Pointer - 1)) {
            return
$this->Error(6);
        }
        
$this->mRowData = mysql_fetch_array($this->mQueryId, MYSQL_BOTH);
        
$this->mPointer = $_Pointer;
        return
TRUE;
    }

    
/**
     * @ Purpose :
     * 偏移指针位置。
     * @ Method Name : MoveBy()
     * @ Parameters :
     * integer $_Pointer 指针位置的偏移量
     * @ Return : boolean 设置是否成功
     */
    
function MoveBy($_Pointer = 1) {
        if (
$this->IsError()) return FALSE;
        if (!
is_int($_Pointer)) $_Pointer = intval($_Pointer);
        
$_Pointer += $this->mPointer;
        return
$this->MoveTo($_Pointer);
    }

    
/**
     * @ Purpose :
     * 偏移指针位置至最初。
     * @ Method Name : MoveToBegin()
     * @ Return : boolean 设置是否成功
     */
    
function MoveToBegin() {
        if (
$this->IsError()) return FALSE;
        return
$this->MoveTo();
    }

    
/**
     * @ Purpose :
     * 偏移指针位置至最后。
     * @ Method Name : MoveToEnd()
     * @ Return : boolean 设置是否成功
     */
    
function MoveToEnd() {
        if (
$this->IsError()) return FALSE;
        return
$this->MoveTo($this->mRowTotal);
    }

    
/**
     * @ Purpose :
     * 获取指针位置数据集的数组形式数据
     * @ Method Name : GetArray()
     * @ Parameters :
     * constant $_FetchMode PHP 内置 MySQL 数据集采集模式
     * @ Return : mixed 数据数组
     */
    
function GetArray($_FetchMode = 8486) {
        if (
$this->IsError()) return FALSE;
        if (
$_FetchMode != MYSQL_ASSOC
            
&& $_FetchMode != MYSQL_NUM
            
&& $_FetchMode != MYSQL_BOTH)
            
$_FetchMode = $this->mFetchMode;
        
$t_arr = array();
        while (list(
$k, $v) = each($this->mRowData)) {
            if (
is_int($k)) {
                if (
$_FetchMode != MYSQL_ASSOC) $t_arr[$k] = $v;
            } else {
                if (
$_FetchMode != MYSQL_NUM) $t_arr[$k] = $v;
            }
        }
        return
$t_arr;
    }

    
/**
     * @ Purpose :
     * 获取指针位置数据集的对象形式数据
     * @ Method Name : GetObject()
     * @ Return : mixed 数据对象
     */
    
function & GetObject() {
        if (
$this->IsError()) return FALSE;
        while (list(
$k, $v) = each($this->mRowData)) {
            if (!
is_int($k)) {
                
$t_obj->$k = $v;
            }
        }
        return
$t_obj;
    }

    
/**
     * @ Purpose :
     * 获取指针位置数据集的指定字段数据
     * @ Method Name : GetField()
     * @ Parameters :
     * mixed $_Field 确定字段的编号或名称
     * @ Return : mixed 字段信息
     */
    
function GetField($_Field = '') {
        if (
$this->IsError()) return FALSE;
        if (!isset(
$this->mRowData[$_Field])) return FALSE;
        return
$this->mRowData[$_Field];
    }

    
/*
     * @ Purpose :
     * 判断是否在检索数据集最开始
     * @ Method Name : AtBegin()
     * @ Return : boolean 是否在最开始
     */
    
function AtBegin() {
        if (
$this->IsError()) return FALSE;
        return (
$this->mRowTotal && $this->mPointer == 1);
    }

    
/*
     * @ Purpose :
     * 判断是否在检索数据集最末尾
     * @ Method Name : AtEnd()
     * @ Return : boolean 是否在最末尾
     */
    
function AtEnd() {
        if (
$this->IsError()) return FALSE;
        return (
$this->mRowTotal && $this->mPointer == $this->mRowTotal);
    }

}

/** 类封装结束 */
}

?>


阅读全文
0 0

相关文章推荐

img
取 消
img