403_3@
PHP@H_
403_3@/*@H_
403_3@
数据库操作类@H_
403_3@*/@H_
403_3@class
MysqL{@H_
403_3@private $LocalHost = 'localhost';@H_
403_3@private $LoaclUser = 'root';@H_
403_3@private $LocalPass = '123456';@H_
403_3@private $LocalBase = 'jiangxibaiyi';@H_
403_3@private $LocalCode = 'UTF8';@H_
403_3@private $PreFix;@H_
403_3@private $Conn;@H_
403_3@private $Start = 0;@H_
403_3@private $Error = false; //
数据库连接状态,false表示未连接或连接不正常@H_
403_3@public $Err = true; //
sql执行结果private $Table;@H_
403_3@private $Field = '*';@H_
403_3@private $Where = '';@H_
403_3@private $Order = '';private $PageSize = 0; //
分页显示->每页多少条,0为不
分页显示@H_
403_3@private $PageCount = 1; //
分页显示->总共有多少条@H_
403_3@private $PageNum = 1; //
分页显示->总共有多少页@H_
403_3@private $PageNo = 1; //
分页显示->当前第几页@H_
403_3@private $PageKey = 'page'; //
分页url参数键@H_
403_3@private $PageStart = 0; //
分页显示->当前从第几条开始返回@H_
403_3@private $Select;@H_
403_3@private $Rest;private $Result = false;//结果集public $FormArray = array();public $Instr_ID = 0;@H_
403_3@private $j = 0;@H_
403_3@public function Parameter($Loca,$Root,$Pass,$Base,$Code,$PreFix = ''){@H_
403_3@$this->LoaclUser = $Root;@H_
403_3@$this->LocalBase = $Base;@H_
403_3@$this->LocalCode = $Code;@H_
403_3@$this->LocalHost = $Loca;@H_
403_3@$this->LocalPass = $Pass;@H_
403_3@$this->PreFix = $PreFix;@H_
403_3@return $this;@H_
403_3@}@H_
403_3@private function Connection( $
sql ){@H_
403_3@!function_exists(
MysqLi_connect) ? die('
查询失败,无法加载
MysqLi扩展') : null;@H_
403_3@$this->Conn = @new
MysqLi( $this->LocalHost,$this->LoaclUser,$this->LocalPass,$this->LocalBase);@H_
403_3@$this->Error =
MysqLi_connect_errno() == 0 ? true : false;@H_
403_3@!$this->Error ? die('
数据库连接
错误,请检查
数据库连接参数') : null;@H_
403_3@$this->Conn->query('SET NAMES ' . $this->LocalCode);@H_
403_3@$this->Rest = $this->Conn->query($
sql);@H_
403_3@$this->Err =
MysqLi_error($this->Conn);@H_
403_3@$this->Instr_ID =
MysqLi_insert_id($this->Conn);@H_
403_3@$this->Rest->free_result;@H_
403_3@$this->Conn->close;@H_
403_3@$this -> FormArray = '';@H_
403_3@return $this;@H_
403_3@}public function null(){@H_
403_3@$this->PageSize = 0;@H_
403_3@//$this->PageCount = 1;@H_
403_3@$this->PageStart = 1;@H_
403_3@$this->Field = ' * ';@H_
403_3@ $this->Select = '';@H_
403_3@unset($this->Table,$this->Where,$this->Order,$this->Result);@H_
403_3@}public function Table( $TableName ) {//数据表@H_
403_3@ $this -> null();@H_
403_3@ $this->Table = '`' . $this->PreFix . $TableName . '`';@H_
403_3@ return $this;@H_
403_3@}public function Field( $Array = '*' ) {//数据字段@H_
403_3@ !empty( $this->Field ) ? $this->Field = '' : null;@H_
403_3@ $Array = explode(',',$Array);@H_
403_3@ foreach ( $Array as $field ) {@H_
403_3@ $this->Field .= !$this->Start ? '`' . $field . '`' : ',`' . $field . '`';@H_
403_3@ $this->Start++;@H_
403_3@ }@H_
403_3@ $this->Start = 0;@H_
403_3@ return $this;@H_
403_3@}public function Where( $Where ) {//条件@H_
403_3@ $this->Where = ' where ' .$Where;@H_
403_3@ return $this; @H_
403_3@}public function Order( $Order ) {//排序@H_
403_3@ $this->Order = ' order by ' . $Order;@H_
403_3@ return $this;@H_
403_3@}public function pk( $key ) {//
分页url参数键@H_
403_3@ $this->PageKey = $key;@H_
403_3@ return $this;@H_
403_3@}public function Page( $PageSize ) {//
分页@H_
403_3@ $this->PageSize = $PageSize;@H_
403_3@ $this->PageNo = $this->get( $this->PageKey );@H_
403_3@ $this->PageNo = empty( $this->PageNo ) || !isset( $this->PageNo ) || !is_numeric( $this->PageNo ) || $this->PageNo < 1 ? 1 : $this->PageNo;@H_
403_3@ return $this;@H_
403_3@}public function post( $Key,$Filter = true ){@H_
403_3@return $Filter ? strip_tags($_POST[$Key]) : $_POST[$Key];@H_
403_3@}public function get( $Key,$Filter = true ){@H_
403_3@return $Filter ? strip_tags($_GET[$Key]) : $_GET[$Key];@H_
403_3@}public function Sel(){@H_
403_3@$this->Select = 'Select ' . $this->Field . ' from ' . $this->Table . $this->Where . $this->Order;@H_
403_3@$this->Connection( $this->Select );@H_
403_3@if ( $this->Rest->num_rows ) {@H_
403_3@ while ( $Rs = $this->Rest->fetch_assoc() ) {@H_
403_3@ $this->Result[] = $Rs;@H_
403_3@ }@H_
403_3@}@H_
403_3@ $DataBase = $this->Result;@H_
403_3@ return empty($DataBase) ? false : $DataBase;@H_
403_3@}public function querys( $
sql = '',$Type = 'not',$biao = false ) {@H_
403_3@$this->Select = $
sql;@H_
403_3@$this->Connection( $this->Select );@H_
403_3@if ( $this->Rest->num_rows ) {@H_
403_3@ if ( !$biao ) {@H_
403_3@ while ( $Rs = $this->Rest->fetch_array() ) {@H_
403_3@ $this->Result[] = !preg_match('/^\d+$/i',$Type) ? $Rs : $Rs[ $Type ];@H_
403_3@ }@H_
403_3@} else {@H_
403_3@ while ( $Rs = $this->Rest->fetch_assoc() ) {@H_
403_3@ $this->Result[] = $Rs;@H_
403_3@ }@H_
403_3@}@H_
403_3@}@H_
403_3@ $DataBase = $this->Result;@H_
403_3@ return empty($DataBase) ? false : $DataBase;}public function executes( $
sql = '' ){@H_
403_3@$this->Connection( $
sql );@H_
403_3@return $this->Rest;@H_
403_3@}@H_
403_3@public function exists( $T = '',$F = '',$W = ''){@H_
403_3@if ( empty( $F ) ) { return 0; }@H_
403_3@$cmd = empty( $W ) ? 'Select sum(' . $F . ') as `baiyinum` from `' . $this->PreFix . $T .'`' : 'Select sum(' . $F . ') as `baiyinum` from `' . $this->PreFix . $T .'` Where ' . $W;@H_
403_3@ $this->Connection( $cmd );@H_
403_3@unset( $T,$F,$W,$cmd );@H_
403_3@$Rel = $this->Rest->fetch_array();@H_
403_3@return round( $Rel['baiyinum'],2 );@H_
403_3@}@H_
403_3@public function ExistsTo( $Bili = 10000,$T = '',$cmd );@H_
403_3@$Rel = $this->Rest->fetch_array();@H_
403_3@return round( $Rel['baiyinum'] * $Bili );@H_
403_3@}@H_
403_3@public function Select( $Type = true,$ListNum = 1 ){ //返回记录(数组形式, 返回条数)@H_
403_3@ $this->Select = 'Select ' . $this->Field . ' from ' . $this->Table . $this->Where . $this->Order;@H_
403_3@ if ( is_numeric( $ListNum ) ) { @H_
403_3@if ( $this->PageSize > 0 ) {@H_
403_3@$this->Connection( $this->Select );//执行
查询@H_
403_3@$this->PageCount = $this->Rest->num_rows;//取得记录总数@H_
403_3@$this->PageNum = ceil($this->PageCount / $this->PageSize); //总共有多少页@H_
403_3@$this->PageNo = $this->PageNo > $this->PageNum ? $this->PageNum : $this->PageNo;@H_
403_3@$this->PageStart = ( $this->PageNo - 1 ) * $this->PageSize; //当前从第几条开始返回@H_
403_3@$this->Select .= ' limit ' . $this->PageStart . ',' .$this->PageSize; //重新构造
sql语句@H_
403_3@} else {@H_
403_3@$this->Select .= ' limit ' . $ListNum; //重新构造
sql语句@H_
403_3@}@H_
403_3@ } else {@H_
403_3@$this->Select .= ' limit 1'; //重新构造
sql语句@H_
403_3@ }@H_
403_3@ //echo $this->Select;@H_
403_3@ $this->Connection( $this->Select );//再次执行
查询@H_
403_3@ if ( $this->Rest->num_rows ) {//如果记录存在@H_
403_3@if ( $Type ) {@H_
403_3@ while ( $Rs = $this->Rest->fetch_array() ) {@H_
403_3@ $this->Result[] = $Rs;@H_
403_3@ }@H_
403_3@}else{@H_
403_3@ while ( $Rs = $this->Rest->fetch_assoc() ) {@H_
403_3@ $this->Result[] = $Rs;@H_
403_3@ }@H_
403_3@}@H_
403_3@ }@H_
403_3@ if ( ( $ListNum == 1 or !is_numeric( $ListNum ) ) && !$this->PageSize) { $this->Result = $this->Result[0]; }@H_
403_3@ $DataBase = $this->Result;@H_
403_3@ return empty($DataBase) ? false : $DataBase;@H_
403_3@} public function Num() { //返回记录总数@H_
403_3@ $this->Select = 'Select ' . $this->Field . ' from ' . $this->Table . $this->Where . $this->Order;@H_
403_3@ $this->Connection( $this->Select );//执行
查询@H_
403_3@ return $this->Rest->num_rows;//取得记录总数@H_
403_3@ } public function PageNav($NumNav = false ) { //
分页@H_
403_3@ $Action = $this -> get('action');@H_
403_3@ !empty( $Action ) or $Action = 'index';@H_
403_3@ $Module = $this -> get('module');@H_
403_3@ !empty( $Module ) or $Module = 'index';@H_
403_3@ $NavUrl = '/' . $Module . '/' . $Action . '/' . $this -> PageKey .'/';@H_
403_3@ $NaIndex = '/' . $Module . '/' . $Action;@H_
403_3@ $PageHtml = "\n
403_3@ $PageHtml .= '
' . $this->PageCount . '条记录 ' . $this->PageNo . '/' . $this->PageNum . '页 ';@H_
403_3@ $this->PageNo <= 1 or $PageHtml .= "
\nPageNo - 1) . "\">上一页\n";@H_
403_3@if ( $NumNav ) { $PageHtml .= $this->NumPage($NavUrl); }@H_
403_3@$this->PageNo >= $this->PageNum or $PageHtml .= "
PageNo + 1) . "\">下一页\n
PageNum . "\">尾页\n";@H_
403_3@$PageHtml .= "
\n";@H_
403_3@return $PageHtml; @H_
403_3@ } private function NumPage( $Can = '' ) { //数字
分页@H_
403_3@ $NumHtml = '';@H_
403_3@ $First = 1;@H_
403_3@ $Last = $this->PageNum;@H_
403_3@ if ( $this->PageNum > 5 ) {@H_
403_3@ if ( $this->PageNo < $this->PageNum ) {@H_
403_3@ $First = $this->PageNo - 2;@H_
403_3@ $Last = $this->PageNo + 2;@H_
403_3@ }else{@H_
403_3@ $First = $this->PageNo - 4;@H_
403_3@ $Last = $this->PageNum;@H_
403_3@ }@H_
403_3@ }@H_
403_3@ if ( $First < 1 ) { $First = 1; $Last = $First + 4;}@H_
403_3@ if ( $Last > $this->PageNum ) { $First = $this->PageNum - 4; $Last = $this->PageNum;}@H_
403_3@ for( $i = $First; $i <= $Last; $i++) {@H_
403_3@ $NumHtml .= $this->PageNo != $i ? "\n\t" . '
' . "\n\t" : "\n\t" .'' . "\n\t";@H_403_3@ }@H_
403_3@ unset($Can,$First,$i,$Last);@H_
403_3@ return $NumHtml;@H_
403_3@ } public function UserPage($NumNav = false,$PageName = 'index',$Mulu = 'user' ) { //会员中心
分页@H_
403_3@ $NavUrl = '/' . $Mulu . '/' . $PageName . '/' . $this->PageKey . '/';@H_
403_3@ $PageHtml = "\n
403_3@ $PageHtml .= '
' . $this->PageCount . '条记录 ' . $this->PageNo . '/' . $this->PageNum . '页 ';@H_
403_3@ $this->PageNo <= 1 or $PageHtml .= "
\nPageNo - 1) . "\">上一页\n";@H_
403_3@if ( $NumNav ) { $PageHtml .= $this->NumPage($NavUrl); }@H_
403_3@$this->PageNo >= $this->PageNum or $PageHtml .= "
PageNo + 1) . "\">下一页\n
PageNum . "\">尾页\n";@H_
403_3@$PageHtml .= "
\n";@H_
403_3@return $PageHtml; @H_
403_3@ } @H_
403_3@ //表单处理开始//判断表单时候提交@H_
403_3@public function FormIs( $Keys = 'mm' ) {@H_
403_3@return $_POST[ $Keys ] == 1 ? true : false;@H_
403_3@}//post方式
获取数据@H_
403_3@public function _post( $Keys = '',$TiHuan = '') {@H_
403_3@$Values = strip_tags( $_POST[ $Keys ] );@H_
403_3@$this->FormArray[$Keys] = empty( $Values ) ? $TiHuan : $Values;@H_
403_3@ return empty( $Values ) ? $TiHuan : $Values;@H_
403_3@}//get
方法获取数据@H_
403_3@public function _get( $Keys = '',$TiHuan = '') {@H_
403_3@$Values = strip_tags( $_GET[ $Keys ] );@H_
403_3@ return empty( $Values ) ? $TiHuan : $Values;@H_
403_3@}//判断是否为数字并且不小于0@H_
403_3@public function IsNum( $Num = 0,$Mesg = '参数必须为数字' ) {@H_
403_3@if ( is_numeric( $Num ) && !empty( $Num ) && $Num >= 0 ) {@H_
403_3@return $Num;@H_
403_3@}else{@H_
403_3@die( $Mesg );@H_
403_3@}@H_
403_3@}//判断是否为数字并且不小于0返回True/False@H_
403_3@public function NumBer( $Num = 0) {@H_
403_3@ return is_numeric( $Num ) && !empty( $Num ) && $Num >= 0 ? true : false;@H_
403_3@}//检测相关数据似乎存在@H_
403_3@public function IsData($Types = true,$memg = '数据已经存在' ){@H_
403_3@$this->Connection('select ' . $this->Field . ' from ' . $this->Table . $this->Where);@H_
403_3@if ( $Types ){@H_
403_3@$this->Rest->num_rows > 0 ? die( $memg ) : null;@H_
403_3@} else {@H_
403_3@return $this->Rest->num_rows;@H_
403_3@}@H_
403_3@}@H_
403_3@//写入
数据库记录@H_
403_3@public function into( $Mesg = '' ){@H_
403_3@!is_array( $this->FormArray ) ? die( $Mesg ) : null;@H_
403_3@$
sql = 'insert into ' . $this->Table . ' (`';@H_
403_3@$I = 0;@H_
403_3@foreach ( $this->FormArray as $Key => $Val ){@H_
403_3@$Duan .= !$I ? $Key . '`' : ',`' . $Key . '`';@H_
403_3@if ( is_numeric( $Val ) ){@H_
403_3@$Vals .= !$I ? $Val : ',' . $Val;@H_
403_3@}else{@H_
403_3@$Vals .= !$I ? '\'' . $Val . '\'' : ',\'' . $Val . '\'';@H_
403_3@}@H_
403_3@$I++;@H_
403_3@}@H_
403_3@$
sql .= $Duan . ') values (' . $Vals . ')';//@file_put_contents('1.
sql',$
sql,FILE_APPEND);$this->Connection( $
sql );@H_
403_3@return !empty( $this->Err ) ? false : true;@H_
403_3@} //数组形式写入数据@H_
403_3@ public function Msg
Box( $Table = '',$Filed = array() ) {@H_
403_3@ $this -> Table($Table);@H_
403_3@ foreach( $Filed as $Key => $Val ) {@H_
403_3@ $this -> FormArray[ $Key ] = $Val; @H_
403_3@ }@H_
403_3@ return $this -> Into('未取得数据');@H_
403_3@ } //
修改数据库记录@H_
403_3@ public function Edit( $Array = array() ) {@H_
403_3@ if ( empty( $Array ) ) { $Array = $this -> FormArray; }@H_
403_3@ if ( !is_array( $Array ) || empty( $Array ) ) {@H_
403_3@ return false;@H_
403_3@ } else {@H_
403_3@ $
sql = 'update ' . $this -> Table . ' set ';@H_
403_3@ $I = 0;@H_
403_3@ $Sub = '';@H_
403_3@ $Huan = array('-' => '[jian]','+' => '[jia]','*' => '[cheng]','/' => '[chu]');@H_
403_3@ $Zhan = array('[jian]' => '-','[jia]' => '+','[cheng]' => '*','[chu]' => '/');
foreach ( $Array as $Files => $Val ) {@H_403_3@ $Val = !is_numeric( $Val ) && !preg_match('/\`\w+\`\s*(\+|\-|\*|\/)/i',$Val) ? '\'' . $Val . '\'' : $Val;@H_403_3@ foreach ( $Huan as $key => $val ){@H_403_3@ $Val = str_replace($key,$val,$Val);@H_403_3@ }@H_403_3@ $duan = !$I ? '`' . $Files . '` = ' : ',`' . $Files . '` = ';@H_403_3@ $Sub .= $duan . $Val;@H_403_3@ $I++;@H_403_3@ }@H_403_3@ $sql .= $Sub . $this -> Where;@H_403_3@ foreach ( $Zhan as $Fan => $Hui ) {@H_403_3@ $sql = str_replace($Fan,$Hui,$sql);@H_403_3@ } //echo $sql; die; $this -> Connection( $sql );@H_403_3@ unset( $Array,$duan,$Fan,$Files,$Huan,$I,$key,$Sub,$Val,$Zhan,$val );@H_403_3@ return !empty( $this -> Err ) ? false : true;@H_403_3@ }@H_403_3@ } //删除数据库记录@H_403_3@ public function del(){@H_403_3@ $sql = 'delete from ' . $this->Table . $this->Where;@H_403_3@ $this->Connection( $sql );@H_403_3@ unset($sql);@H_403_3@ return !empty( $this->Err ) ? false : true;@H_403_3@ } //表单处理结束 //页面跳转@H_403_3@ public function Msg( $Text = '操作成功' ) {@H_403_3@ echo '';@H_403_3@ echo '';@H_403_3@exit;@H_403_3@ } #取得系统当前时间@H_403_3@ public function Times(){@H_403_3@ return str_replace('-','[jian]',date('Y-m-d H:i:s'));@H_403_3@ } #取得用户IP地址@H_403_3@public function GetIP(){ @H_403_3@ if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"),"unknown")) @H_403_3@ $ip = getenv("HTTP_CLIENT_IP"); @H_403_3@else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"),"unknown")) @H_403_3@ $ip = getenv("HTTP_X_FORWARDED_FOR"); @H_403_3@else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"),"unknown")) @H_403_3@ $ip = getenv("REMOTE_ADDR"); @H_403_3@else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'],"unknown")) @H_403_3@ $ip = $_SERVER['REMOTE_ADDR']; @H_403_3@else @H_403_3@ $ip = "unknown"; @H_403_3@ return($ip); @H_403_3@} @H_403_3@ //最后关闭数据库连接@H_403_3@ public function Close(){@H_403_3@ !is_object( $this -> Conn ) or MysqLi_close( $this -> Conn );@H_403_3@ }}@H_403_3@
使用方法:@H_403_3@声明数据库对象@H_403_3@$Conn = new MysqL;@H_403_3@加载数据库参数@H_403_3@$Conn->Parameter(数据库服务器,数据库用户名,数据库密码,数据库名称,数据库编码,数据库表前缀[可为空]);
上面的代码就已经加载这个类文件进来了,并且初始化了一些数据库连接参数!
下面介绍几个基本是方法函数:@H_403_3@1、 $Conn -> Table();@H_403_3@选择数据表,参数是数据表名称@H_403_3@2、$Conn -> Field();@H_403_3@选择的字段名称,多个用逗号隔开,如不调用这个方法,则返回全部@H_403_3@3、$Conn -> Where();@H_403_3@sql Where子语句,根据条件筛选@H_403_3@4、$Conn -> Order();@H_403_3@sql 排序@H_403_3@5、$Conn -> Page(int);@H_403_3@参数是一个正整数数字,如调用这个方法,记录将分页显示@H_403_3@6、$Conn -> Select(布尔值);@H_403_3@执行查询,返回查询结果,如果有,则是一个二维数组,如果无,则返回假,参数可省略,如省略,默认为真,返回的数组包含数字元素@H_403_3@7、$Conn -> Del();@H_403_3@删除记录@H_403_3@8、 $Conn -> Edit(array());@H_403_3@修改记录,参数是一个一维数组,数组键是字段名称,数组值是字段值@H_403_3@9、$Conn -> Into(array());@H_403_3@添加记录,参数是一个一维数组,数组键是字段名称,数组值是字段值。
以上方法可连续调用,比如:@H_403_3@
403_3@$Rult = $Conn -> Table('user') -> Select(); //
查询返回user表的所有记录@H_
403_3@$Rult = $Conn -> Table('user') -> Page(20) -> Select();//
查询返回user表的所有记录,并
分页显示,每页20条;@H_
403_3@
@H_403_3@