[原创]简单快速有趣的MySQL数据库操作类:SimpleDB

自己写着玩的,代码没有测试,不过觉得思路不错,如果能够加上部分异常处理的功能,应该比较帅了,支持PHP4/PHP5,恩,虽然没有ADOdb或者PEAR::DB强,不错一般应用应该不错,恩。

喜欢的就自己拿去用吧,自己随便改,呵呵,也欢迎提意见。(注释遵循PHPDoc的标准,便于生成手册)

注意:代码未经测试,出现问题可要自己负责哇,呵呵。

<?
// ==========================================
//文件:SimpleDB.class.php
//程序:MySQL数据库操作类
//作者:heiyeluren<http://blog.csdn.net/heiyeshuwu
//时间:2006-09-20
//==========================================


class SimpleDB
{
/* *
*连接标识
*@varresource
*/
var $dbLink ;
/* *
*数据库查询语句
*@varstring
*/
var $dbSql ;
/* *
*查询结果
*@varresource
*/
var $dbResult ;
/* *
*查询记录集
*@vararray
*/
var $dbRecord ;
/* *
*数据库字符集
*@varstring
*/
var $dbCharset = ' GBK ' ;
/* *
*数据库结果集提取方式
*@varint
*/
var $fetchMode = MYSQL_ASSOC;
/* *
*日志保存路径
*@varstring
*/
var $logPath = ' /tmp/mysql_log ' ;

/* *
*是否记录SQL查询失败的SQL日志,缺省是false
*@varbool
*/
var $isLog = false ;
/* *
*是否在SQL查询出错的时候显示错误并且终止脚本执行,缺省是true
*
*@varbool
*/
var $isError = true ;


// --------------------------
//内部接口
//--------------------------

/* *
*构造函数
*
*@paramstring$db_host连接主机
*@paramstring$db_user连接用户
*@paramstring$db_passwd数据库密码
*@paramstring$db_name数据库
*@parambool$is_pconnect是否长连接,默认是否
*@returnSimpleDB
*/
function SimpleDB( $db_host , $db_user , $db_passwd , $db_name , $is_pconnect = false ){
$this -> connect( $db_host , $db_user , $db_passwd , $db_name , $is_pconnect );
}

/* *
*连接数据库
*
*@paramstring$db_host数据库主机地址,例如:localhost,或者localhost:3306
*@paramstring$db_user连接数据库的用户
*@paramstring$db_passwd用户密码
*@paramstring$db_name数据库名字
*@paramboo$is_pconnect是否使用长连接
*@returnresource返回连接资源标识符
*/
function connect( $db_host , $db_user , $db_passwd , $db_name , $is_pconnect ){
if ( $is_pconnect ){
return $this -> dbLink = @ mysql_pconnect ( $db_host , $db_user , $db_passwd );
}
$this -> dbLink = @ mysql_connect ( $db_host , $db_user , $db_passwd );
@
mysql_select_db ( $db_name , $this -> dbLink);
$mysql_version = $this -> getOne( " SELECTVERSION() " );
if ( $this -> dbCharset != '' && preg_match ( " /^(5.|4.1)/ " , $mysql_version )){
$this -> query( " SETNAMES'$this->dbCharset' " , $this -> dbLink);
}
return $this -> dbLink;
}

/* *
*关闭数据库连接
*
*@returnbool是否成功关闭连接
*/
function disconnect(){
$ret = @ mysql_close ( $this -> dbLink);
$this -> dbLink = null ;
return $ret ;
}

/* *
*设置查询结果返回数据类型
*
*@paramint$modeType设置查询结果返回设置,1为关联索引和数字所有都有,2为使用关联索引,3为使用数字索引
*/
function setFetchMode( $modeType ){
switch ( $modeType ){
case 1 : // 数字索引和关联索引都有
$this -> fetchMode = MYSQL_BOTH;
break ;
case 2 : // 使用关联索引
$this -> fetchMode = MYSQL_ASSOC;
break ;
case 3 : // 使用数字索引
$this -> fetchMode = MYSQL_NUM;
break ;
default : // 缺省使用关联索引
$this -> fetchMode = MYSQL_ASSOC;
}
}

/* *
*设置数据库客户端提取结果集的字符编码
*
*@paramstring$charset编码的字符串,比如UTF8,GBK之类的,缺省是GBK
*/
function setCharset( $charset ){
if ( $charset != '' ){
$this -> dbCharset = $charset ;
}
}

/* *
*设置日志存储路径
*
*@paramstring$log_path日志路径,该必须是可写的
*/
function setLogPath( $log_path ){
if ( $log_path != '' ){
$this -> logPath = $log_path ;
}
}

/* *
*写SQL执行日志
*
*@paramstring$sql查询的SQL语句
*@paramstring$file当前执行查询的文件
*/
function writeLog( $sql , $file ){
if ( ! file_exists ( $this -> logPath)){
@
mkdir ( $this -> logPath);
}
$log_file = $this -> logPath . " /mysql_ " . date ( " Y-m-d " ) . " .log " ;
$log_msg = " [ " . date ( " Y-m-dH:i:s " ) . " ]- " . $file . " : " . $sql . " " ;
error_log ( $log_msg , 3 , $log_file );
}

/* *
*显示上依次SQL执行错误的错误信息
*/
function showError(){
$errMessage = " MySQLqueryerror " . mysql_errno ( $this -> dbLink) . " : " . mysql_error ( $this -> dbLink);
die ( $errMessage );
}

/* *
*返回MySQL的版本信息
*
*@returnstringMysql的版本
*/
function getVersion(){
return $this -> getOne( " SELECTVERSION() " );
}

/* *
*查询操作的底层接口
*
*@paramstring$sql要执行查询的SQL语句
*@returnbool执行成功返回true,失败返回false
*/
function query( $sql ){
$this -> dbSql = $sql ;
$this -> dbResult = null ;
$this -> dbResult = @ mysql_query ( $sql , $this -> dbLink);
if ( $this -> dbResult === false ){
if ( $this -> isLog){
$this -> writeLog( $sql , __FILE__ );
}
if ( $this -> isError){
$this -> showError();
}
return false ;
}
return true ;
}


// --------------------------
//数据获取接口
//--------------------------

/* *
*获取SQL执行的全部结果集(二维数组)
*
*@paramstring$sql需要执行查询的SQL语句
*@return成功返回查询结果的二维数组,失败返回false
*/
function getAll( $sql ){
if ( ! $this -> query( $sql )){
return false ;
}
$this -> dbRecord = array ();
while ( $row = @ mysql_fetch_array ( $this -> dbResult , $this -> fetchMode)){
$this -> dbRecord[] = $row ;
}
@
mysql_free_result ( $this -> dbResult);
if ( ! is_array ( $this -> dbRecord) || empty ( $this -> dbRecord)){
return false ;
}
return $this -> dbRecord;
}

/* *
*获取单行记录(一维数组)
*
*@paramstring$sql需要执行查询的SQL语句
*@return成功返回结果记录的一维数组,失败返回false
*/
function getRow( $sql ){
if ( ! $this -> query( $sql )){
return false ;
}
$this -> dbRecord = array ();
$this -> dbRecord = @ mysql_fetch_array ( $this -> dbResult , $this -> fetchMode);
@
mysql_free_result ( $this -> dbResult);
if ( ! is_array ( $this -> dbRecord) || empty ( $this -> dbRecord)){
return false ;
}
return $this -> dbRecord;
}

/* *
*获取一列数据(一维数组)
*
*@paramstring$sql需要获取的字符串
*@paramstring$field需要获取的列,如果不指定,默认是第一列
*@return成功返回提取的结果记录的一维数组,失败返回false
*/
function getCol( $sql , $field = '' ){
if ( ! $this -> query( $sql )){
return false ;
}
$this -> dbRecord = array ();
while ( $row = @ mysql_fetch_array ( $this -> dbResult , $this -> fetchMode)){
if ( trim ( $field ) == '' ){
$this -> dbRecord[] = current ( $row );
}
else {
$this -> dbRecord[] = $row [ $field ];
}
}
@
mysql_free_result ( $this -> dbResult);
if ( ! is_array ( $this -> dbRecord) || empty ( $this -> dbRecord)){
return false ;
}
return $this -> dbRecord;
}

/* *
*获取一个数据(当条数组)
*
*@paramstring$sql需要执行查询的SQL
*@return成功返回获取的一个数据,失败返回false
*/
function getOne( $sql , $field = '' ){
if ( ! $this -> query( $sql )){
return false ;
}
$this -> dbRecord = array ();
$row = @ mysql_fetch_array ( $this -> dbResult , $this -> fetchMode);
@
mysql_free_result ( $this -> dbResult);
if ( ! is_array ( $row ) || empty ( $row )){
return false ;
}
if ( trim ( $field ) != '' ){
$this -> dbRecord = $row [ $field ];
}
else {
$this -> dbRecord = current ( $row );
}
return $this -> dbRecord;
}

/* *
*获取指定各种条件的记录
*
*@paramstring$table表名(访问的数据表)
*@paramstring$field字段(要获取的字段)
*@paramstring$where条件(获取记录的条件语句,不包括WHERE,默认为空)
*@paramstring$order排序(按照什么字段排序,不包括ORDERBY,默认为空)
*@paramstring$limit限制记录(需要提取多少记录,不包括LIMIT,默认为空)
*@parambool$single是否只是取单条记录(是调用getRow还是getAll,默认是false,即调用getAll)
*@return成功返回记录结果集的数组,失败返回false
*/
function getRecord( $table , $field = ' * ' , $where = '' , $order = '' , $limit = '' , $single = false ){
$sql = " SELECT$fieldFROM$table " ;
$sql .= trim ( $where ) != '' ? " WHERE$where " : $where ;
$sql .= trim ( $order ) != '' ? " ORDERBY$order " : $order ;
$sql .= trim ( $limit ) != '' ? " LIMIT$limit " : $limit ;
if ( $single ){
return $this -> getRow( $sql );
}
return $this -> getAll( $sql );
}

/* *
*获取指点各种条件的记录(跟getRecored类似)
*
*@paramstring$table表名(访问的数据表)
*@paramstring$field字段(要获取的字段)
*@paramstring$where条件(获取记录的条件语句,不包括WHERE,默认为空)
*@paramarray$order_arr排序数组(格式类似于:array('id'=>true),那么就是按照ID为顺序排序,array('id'=>false),就是按照ID逆序排序)
*@paramarray$limit_arr提取数据的限制数组()
*@returnunknown
*/
function getSpecifyRecord( $table , $field = ' * ' , $where = '' , $order_arr = array () , $limit_arr = array ()){
$sql = " SELECT$fieldFROM$table " ;
$sql .= trim ( $where ) != '' ? " WHERE$where " : $where ;
if ( is_array ( $order_arr ) && ! empty ( $order_arr )){
$arr_key = key ( $order_arr );
$sql .= " ORDERBY$arr_key " . ( $order_arr [ $arr_key ] ? " ASC " : " DESC " );
}
if ( is_array ( $limit_arr ) && ! empty ( $limit_arr )){
$start_post = intval ( array_shift ( $limit_arr ));
$offset = intval ( array_shift ( $limit_arr ));
$sql .= " LIMIT$start_post,$offset " ;
}
return $this -> getAll( $sql );
}

/* *
*获取指定条数的记录
*
*@paramstring$table表名
*@paramint$start_pos开始记录
*@paramint$offset偏移量
*@paramstring$field字段名
*@paramstring$where条件(获取记录的条件语句,不包括WHERE,默认为空)
*@paramstring$order排序(按照什么字段排序,不包括ORDERBY,默认为空)
*@return成功返回包含记录的二维数组,失败返回false
*/
function getLimitRecord( $table , $start_pos , $offset , $field = ' * ' , $where = '' , $oder = '' ){
$sql = " SELECT$fieldFROM$table " ;
$sql .= trim ( $where ) != '' ? " WHERE$where " : $where ;
$sql .= trim ( $order ) != '' ? " ORDERBY$order " : $order ;
$sql .= " LIMIT$start_pos,$offset " ;
return $this -> getAll( $sql );
}

/* *
*获取排序记录
*
*@paramstring$table表名
*@paramstring$order_field需要排序的字段(比如id)
*@paramstring$order_method排序的方式(1为顺序,2为逆序,默认是1)
*@paramstring$field需要提取的字段(默认是*,就是所有字段)
*@paramstring$where条件(获取记录的条件语句,不包括WHERE,默认为空)
*@paramstring$limit限制记录(需要提取多少记录,不包括LIMIT,默认为空)
*@return成功返回记录的二维数组,失败返回false
*/
function getOrderRecord( $table , $order_field , $order_method = 1 , $field = ' * ' , $where = '' , $limit = '' ){
// $order_method的值为1则为顺序,$order_method值为2则2则是逆序排列
$sql = " SELECT$fieldFROM$table " ;
$sql .= trim ( $where ) != '' ? " WHERE$where " : $where ;
$sql .= " ORDERBY$order_field " . ( $order_method == 1 ? " ASC " : " DESC " );
$sql .= trim ( $limit ) != '' ? " LIMIT$limit " : $limit ;
return $this -> getAll( $sql );
}

/* *
*分页查询(限制查询的记录条数)
*
*@paramstring$sql需要查询的SQL语句
*@paramint$start_pos开始记录的条数
*@paramint$offset每次的偏移量,需要获取多少条
*@return成功返回获取结果记录的二维数组,失败返回false
*/
function limitQuery( $sql , $start_pos , $offset ){
$start_pos = intval ( $start_pos );
$offset = intval ( $offset );
$sql = $sql . " LIMIT$start_pos,$offset " ;
return $this -> getAll( $sql );
}


// --------------------------
//无数据返回操作
//--------------------------

/* *
*执行执行非Select查询操作
*
*@paramstring$sql查询SQL语句
*@returnbool成功执行返回true,失败返回false
*/
function execute( $sql ){
if ( ! $this -> query( $sql )){
return false ;
}
$count = @ mysql_affected_rows ( $this -> dbLink);
if ( $count <= 0 ){
return false ;
}
return true ;
}

/* *
*自动执行操作(针对Insert/Update操作)
*
*@paramstring$table表名
*@paramarray$field_array字段数组(数组中的键相当于字段名,数组值相当于值,类似array('id'=>100,'user'=>'heiyeluren')
*@paramint$mode执行操作的模式(是插入还是更新操作,1是插入操作Insert,2是更新操作Update)
*@paramstring$where如果是更新操作,可以添加WHERE的条件
*@returnbool执行成功返回true,失败返回false
*/
function autoExecute( $table , $field_array , $mode , $where = '' ){
if ( $table == '' || ! is_array ( $field_array ) || empty ( $field_array )){
return false ;
}
// $mode为1是插入操作(Insert),$mode为2是更新操作
if ( $mode == 1 ){
$sql = " INSERTINTO$tableSET " ;
}
else {
$sql = " UPDATE$tableSET " ;
}
foreach ( $field_array as $key => $value ){
$sql .= " $key='$value', " ;
}
$sql = rtrim ( $sql , ' , ' );
if ( $mode == 2 && $where != '' ){
$sql .= " WHERE$where " ;
}
return $this -> execute( $sql );
}


// --------------------------
//其他数据相关操作
//--------------------------

/* *
*获取最后一次查询的SQL语句
*
*@returnstring返回最后一次查询的SQL语句
*/
function getLastSql(){
return $this -> dbSql;
}

/* *
*获取上次插入操作的的ID
*
*@returnint如果没有连接或者查询失败,返回0,成功返回ID
*/
function getLastId(){
if ( ! $this -> dbLink){
return 0 ;
}
if (( $last_id = mysql_insert_id ( $this -> dbLink)) > 0 ){
return $last_id ;
}
return $this -> getOne( " SELECTLAST_INSERT_ID() " );
}

/* *
*获取记录集里面的记录条数(用于Select操作)
*
*@returnint如果上一次无结果集或者记录结果集为空,返回0,否则返回结果集数量
*/
function getNumRows(){
if ( ! $this -> dbResult){
return 0 ;
}
return mysql_num_rows ( $this -> dbResult);
}

/* *
*获取受到影响的记录数量(用于Update/Delete/Insert操作)
*
*@returnint如果没有连接或者影响记录为空,否则返回影响的行数量
*/
function getAffectedRows(){
if ( ! $this -> dbLink){
return 0 ;
}
return mysql_affected_rows ( $this -> dbLink);
}
}
?>

你可能感兴趣的:(simple)