php实现mysql数据库操作类分享

前端之家收集整理的这篇文章主要介绍了php实现mysql数据库操作类分享前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

代码如下:
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) . "\">下一页\nPageNum . "\">尾页\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) . "\">下一页\nPageNum . "\">尾页\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 MsgBox( $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@

猜你在找的PHP相关文章