侯体宗的博客
  • 首页
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

PHP实现的通过参数生成MYSQL语句类完整实例

php  /  管理员 发布于 7年前   159

本文实例讲述了PHP实现的通过参数生成MYSQL语句类。分享给大家供大家参考,具体如下:

这个类可以通过指定的表和字段参数创建SELECT ,INSERT , UPDATE 和 DELETE 语句。

这个类可以创建SQL语句的WHERE条件,像LIKE的查询语句,使用LEFT JOIN和ORDER 语句

Result Generate Insert
" ; $object = new MyLibSQLGen(); $object -> clear_all_assign(); // to refresh all property but it no need when first time execute $object -> setFields( $fields ); $object -> setValues( $values ); $object -> setTables( $tables ); if ( ! $object -> getInsertSQL()){ echo $object -> Error; exit ;} else { $sql = $object -> Result; echo $sql . "
" ;} echo " Result Generate Update
" ; $fields = Array ( " name " , " address " , " city " ); $values = Array ( " Fadjar " , " Resultmang Raya Street " , " Jakarta " ); $tables = Array ( " customer " ); $id = 1 ; $conditions [ 0 ][ " condition " ] = " id='$id' " ; $conditions [ 0 ][ " connection " ] = "" ; $object -> clear_all_assign(); $object -> setFields( $fields ); $object -> setValues( $values ); $object -> setTables( $tables ); $object -> setConditions( $conditions ); if ( ! $object -> getUpdateSQL()){ echo $object -> Error; exit ;} else { $sql = $object -> Result; echo $sql . "
" ;} echo " Result Generate Delete
" ; $tables = Array ( " customer " ); $conditions [ 0 ][ " condition " ] = " id='1' " ; $conditions [ 0 ][ " connection " ] = " OR " ; $conditions [ 1 ][ " condition " ] = " id='2' " ; $conditions [ 1 ][ " connection " ] = " OR " ; $conditions [ 2 ][ " condition " ] = " id='4' " ; $conditions [ 2 ][ " connection " ] = "" ; $object -> clear_all_assign(); $object -> setTables( $tables ); $object -> setConditions( $conditions ); if ( ! $object -> getDeleteSQL()){ echo $object -> Error; exit ;} else { $sql = $object -> Result; echo $sql . "
" ;} echo " Result Generate List
" ; $fields = Array ( " id " , " name " , " address " , " city " ); $tables = Array ( " customer " ); $id = 1 ; $conditions [ 0 ][ " condition " ] = " id='$id' " ; $conditions [ 0 ][ " connection " ] = "" ; $object -> clear_all_assign(); $object -> setFields( $fields ); $object -> setTables( $tables ); $object -> setConditions( $conditions ); if ( ! $object -> getQuerySQL()){ echo $object -> Error; exit ;} else { $sql = $object -> Result; echo $sql . "
" ;} echo " Result Generate List with search on all fields
" ; $fields = Array ( " id " , " name " , " address " , " city " ); $tables = Array ( " customer " ); $id = 1 ; $search = " Fadjar Nurswanto " ; $object -> clear_all_assign(); $object -> setFields( $fields ); $object -> setTables( $tables ); $object -> setSearch( $search ); if ( ! $object -> getQuerySQL()){ echo $object -> Error; exit ;} else { $sql = $object -> Result; echo $sql . "
" ;} echo " Result Generate List with search on some fields
" ; $fields = Array ( " id " , " name " , " address " , " city " ); $tables = Array ( " customer " ); $id = 1 ; $search = Array ( " name " => " Fadjar Nurswanto " , " address " => " Tomang Raya " ); $object -> clear_all_assign(); $object -> setFields( $fields ); $object -> setTables( $tables ); $object -> setSearch( $search ); if ( ! $object -> getQuerySQL()){ echo $object -> Error; exit ;} else { $sql = $object -> Result; echo $sql . "
" ;}?>

类代码:

DATE      : 2006-08-02PRODUCTNAME    : class MyLibSQLGenPRODUCTVERSION  : 1.0.0DESCRIPTION    : class yang berfungsi untuk menggenerate SQLDENPENCIES    : */  class MyLibSQLGen{   var  $Result ;   var  $Tables = Array ();   var  $Values = Array ();   var  $Fields = Array ();   var  $Conditions = Array ();   var  $Condition ;   var  $LeftJoin = Array ();   var  $Search ;   var  $Sort = " ASC " ;   var  $Order ;   var  $Error ;   function MyLibSQLGen(){}   function BuildCondition()  {     $funct = " BuildCondition " ;     $className = get_class ( $this );     $conditions = $this -> getConditions();     if ( ! $conditions ){ $this -> dbgDone( $funct ); return  true ;}     if ( ! is_array ( $conditions ))    {       $this -> Error = " $className::$funct Variable conditions not Array " ;       return ;    }     for ( $i = 0 ; $i < count ( $conditions ); $i ++ )    {       $this -> Condition .= $conditions [ $i ][ " condition " ] . "  " . $conditions [ $i ][ " connection " ] . "  " ;    }     return  true ;  }   function BuildLeftJoin()  {     $funct = " BuildLeftJoin " ;     $className = get_class ( $this );     if ( ! $this -> getLeftJoin()){ $this -> Error = " $className::$funct Property LeftJoin was empty " ; return ;}     $LeftJoinVars = $this -> getLeftJoin();     $hasil = false ;     foreach ( $LeftJoinVars  as  $LeftJoinVar )    {      @ $hasil .= " LEFT JOIN " . $LeftJoinVar [ " table " ];       foreach ( $LeftJoinVar [ " on " ] as  $var )      {        @ $condvar .= $var [ " condition " ] . "  " . $var [ " connection " ] . "  " ;      }       $hasil .= " ON ( " . $condvar . " ) " ;       unset ( $condvar );    }     $this -> ResultLeftJoin = $hasil ;     return  true ;  }   function BuildOrder()  {     $funct = " BuildOrder " ;     $className = get_class ( $this );     if ( ! $this -> getOrder()){ $this -> Error = " $className::$funct Property Order was empty " ; return ;}     if ( ! $this -> getFields()){ $this -> Error = " $className::$funct Property Fields was empty " ; return ;}     $Fields = $this -> getFields();     $Orders = $this -> getOrder();     if ( ereg ( " , " , $Orders )){ $Orders = explode ( " , " , $Order );}     if ( ! is_array ( $Orders )){ $Orders = Array ( $Orders );}     foreach ( $Orders  as  $Order )    {       if ( ! is_numeric ( $Order )){ $this -> Error = " $className::$funct Property Order not Numeric " ; return ;}       if ( $Order  >  count ( $this -> Fields)){ $this -> Error = " $className::$funct Max value of property Sort is " . count ( $this -> Fields); return ;}      @ $xorder .= $Fields [ $Order ] . " , " ;    }     $this -> ResultOrder = " ORDER BY " . substr ( $xorder , 0 ,- 1 );     return  true ;  }   function BuildSearch()  {     $funct = " BuildSearch " ;     $className = get_class ( $this );     if ( ! $this -> getSearch()){ $this -> Error = " $className::$funct Property Search was empty " ; return ;}     if ( ! $this -> getFields()){ $this -> Error = " $className::$funct Property Fields was empty " ; return ;}     $Fields = $this -> getFields();     $xvalue = $this -> getSearch();     if ( is_array ( $xvalue ))    {       foreach ( $Fields  as  $field )      {         if (@ $xvalue [ $field ])        {           $Values = explode ( "  " , $xvalue [ $field ]);           foreach ( $Values  as  $Value )          {@ $hasil .= $field . " LIKE '% " . $Value . " %' OR " ;          }           if ( $hasil )          {@ $hasil_final .= " ( " . substr ( $hasil , 0 ,- 4 ) . " ) AND " ; unset ( $hasil );          }        }      }       $hasil = $hasil_final ;    }     else     {       foreach ( $Fields  as  $field )      {         $Values = explode ( "  " , $xvalue );         foreach ( $Values  as  $Value )        {          @ $hasil .= $field . " LIKE '% " . $Value . " %' OR " ;        }      }    }     $this -> ResultSearch = substr ( $hasil , 0 ,- 4 );     return  true ;  }   function clear_all_assign()  {     $this -> Result = null ;     $this -> ResultSearch = null ;     $this -> ResultLeftJoin = null ;     $this -> Result = null ;     $this -> Tables = Array ();     $this -> Values = Array ();     $this -> Fields = Array ();     $this -> Conditions = Array ();     $this -> Condition = null ;     $this -> LeftJoin = Array ();     $this -> Sort = " ASC " ;     $this -> Order = null ;     $this -> Search = null ;     $this -> fieldSQL = null ;     $this -> valueSQL = null ;     $this -> partSQL = null ;     $this -> Error = null ;     return  true ;  }   function CombineFieldValue( $manual = false )  {     $funct = " CombineFieldsPostVar " ;     $className = get_class ( $this );     $fields = $this -> getFields();     $values = $this -> getValues();     if ( ! is_array ( $fields ))    {       $this -> Error = " $className::$funct Variable fields not Array " ;       return ;    }     if ( ! is_array ( $values ))    {       $this -> Error = " $className::$funct Variable values not Array " ;       return ;    }     if ( count ( $fields ) != count ( $values ))    {       $this -> Error = " $className::$funct Count of fields and values not match " ;       return ;    }     for ( $i = 0 ; $i < count ( $fields ); $i ++ )    {      @ $this -> fieldSQL .= $fields [ $i ] . " , " ;       if ( $fields [ $i ] ==  " pwd "  ||  $fields [ $i ] ==  " password "  ||  $fields [ $i ] ==  " pwd " )      {        @ $this -> valueSQL .= " password(' " . $values [ $i ] . " '), " ;        @ $this -> partSQL .= $fields [ $i ] . " =password(' " . $values [ $i ] . " '), " ;      }       else       {         if ( is_numeric ( $values [ $i ]))        {          @ $this -> valueSQL .= $values [ $i ] . " , " ;          @ $this -> partSQL .= $fields [ $i ] . " = " . $values [ $i ] . " , " ;        }         else         {          @ $this -> valueSQL .= " ' " . $values [ $i ] . " ', " ;          @ $this -> partSQL .= $fields [ $i ] . " =' " . $values [ $i ] . " ', " ;        }      }    }     $this -> fieldSQL = substr ( $this -> fieldSQL , 0 ,- 1 );     $this -> valueSQL = substr ( $this -> valueSQL , 0 ,- 1 );     $this -> partSQL = substr ( $this -> partSQL , 0 ,- 1 );     return  true ;  }   function getDeleteSQL()  {     $funct = " getDeleteSQL " ;     $className = get_class ( $this );     $Tables = $this -> getTables();     if ( ! $Tables  ||  ! count ( $Tables ))    {       $this -> dbgFailed( $funct );       $this -> Error = " $className::$funct Table was empty " ;       return ;    }     for ( $i = 0 ; $i < count ( $Tables ); $i ++ )    {      @ $Table .= $Tables [ $i ] . " , " ;    }     $Table = substr ( $Table , 0 ,- 1 );     $sql = " DELETE FROM " . $Table ;     if ( $this -> getConditions())    {       if ( ! $this -> BuildCondition()){ $this -> dbgFailed( $funct ); return ;}       $sql .= " WHERE " . $this -> getCondition();    }     $this -> Result = $sql ;     return  true ;  }   function getInsertSQL()  {     $funct = " getInsertSQL " ;     $className = get_class ( $this );     if ( ! $this -> getValues()){ $this -> Error = " $className::$funct Property Values was empty " ; return ;}     if ( ! $this -> getFields()){ $this -> Error = " $className::$funct Property Fields was empty " ; return ;}     if ( ! $this -> getTables()){ $this -> Error = " $className::$funct Property Tables was empty " ; return ;}     if ( ! $this -> CombineFieldValue()){ $this -> dbgFailed( $funct ); return ;}     $Tables = $this -> getTables();     $sql = " INSERT INTO " . $Tables [ 0 ] . " ( " . $this -> fieldSQL . " ) VALUES ( " . $this -> valueSQL . " ) " ;     $this -> Result = $sql ;     return  true ;  }   function getUpdateSQL()  {     $funct = " getUpdateSQL " ;     $className = get_class ( $this );     if ( ! $this -> getValues()){ $this -> Error = " $className::$funct Property Values was empty " ; return ;}     if ( ! $this -> getFields()){ $this -> Error = " $className::$funct Property Fields was empty " ; return ;}     if ( ! $this -> getTables()){ $this -> Error = " $className::$funct Property Tables was empty " ; return ;}     if ( ! $this -> CombineFieldValue()){ $this -> dbgFailed( $funct ); return ;}     if ( ! $this -> BuildCondition()){ $this -> dbgFailed( $funct ); return ;}     $Tables = $this -> getTables();     $sql = " UPDATE " . $Tables [ 0 ] . " SET " . $this -> partSQL . " WHERE " . $this -> getCondition();     $this -> Result = $sql ;     return  true ;  }   function getQuerySQL()  {     $funct = " getQuerySQL " ;     $className = get_class ( $this );     if ( ! $this -> getFields()){ $this -> Error = " $className::$funct Property Fields was empty " ; return ;}     if ( ! $this -> getTables()){ $this -> Error = " $className::$funct Property Tables was empty " ; return ;}     $Fields = $this -> getFields();     $Tables = $this -> getTables();     foreach ( $Fields  as  $Field ){@ $sql_raw .= $Field . " , " ;}     foreach ( $Tables  as  $Table ){@ $sql_table .= $Table . " , " ;}     $this -> Result = " SELECT " . substr ( $sql_raw , 0 ,- 1 ) . " FROM " . substr ( $sql_table , 0 ,- 1 );     if ( $this -> getLeftJoin())    {       if ( ! $this -> BuildLeftJoins()){ $this -> dbgFailed( $funct ); return ;}       $this -> Result .= "  " . $this -> ResultLeftJoin;    }     if ( $this -> getConditions())    {       if ( ! $this -> BuildCondition()){ $this -> dbgFailed( $funct ); return ;}       $this -> Result .= " WHERE ( " . $this -> Condition . " ) " ;    }     if ( $this -> getSearch())    {       if ( ! $this -> BuildSearch()){ $this -> dbgFailed( $funct ); return ;}       if ( $this -> ResultSearch)      {         if ( eregi ( " WHERE " , $this -> Result)){ $this -> Result .= " AND " . $this -> ResultSearch;}         else { $this -> Result .= " WHERE " . $this -> ResultSearch;}      }    }     if ( $this -> getOrder())    {       if ( ! $this -> BuildOrder()){ $this -> dbgFailed( $funct ); return ;}       $this -> Result .= "  " . $this -> ResultOrder;    }     if ( $this -> getSort())    {       if (@ $this -> ResultOrder)      {         $this -> Result .= "  " . $this -> getSort();      }    }     return  true ;  }   function getCondition(){ return @ $this -> Condition;}   function getConditions(){ if ( count (@ $this -> Conditions) &&  is_array (@ $this -> Conditions)){ return @ $this -> Conditions;}}   function getFields(){ if ( count (@ $this -> Fields) &&  is_array (@ $this -> Fields)){ return @ $this -> Fields;}}   function getLeftJoin(){ if ( count (@ $this -> LeftJoin) &&  is_array (@ $this -> LeftJoin)){ return @ $this -> LeftJoin;}}   function getOrder(){ return @ $this -> Order;}   function getSearch(){ return @ $this -> Search;}   function getSort(){ return @ $this -> Sort ;}   function getTables(){ if ( count (@ $this -> Tables) &&  is_array (@ $this -> Tables)){ return @ $this -> Tables;}}   function getValues(){ if ( count (@ $this -> Values) &&  is_array (@ $this -> Values)){ return @ $this -> Values;}}   function setCondition( $input ){ $this -> Condition = $input ;}   function setConditions( $input )  {     if ( is_array ( $input )){ $this -> Conditions = $input ;}     else { $this -> Error = get_class ( $this ) . " ::setConditions Parameter input not array " ; return ;}  }   function setFields( $input )  {     if ( is_array ( $input )){ $this -> Fields = $input ;}     else { $this -> Error = get_class ( $this ) . " ::setFields Parameter input not array " ; return ;}  }   function setLeftJoin( $input )  {     if ( is_array ( $input )){ $this -> LeftJoin = $input ;}     else { $this -> Error = get_class ( $this ) . " ::setFields Parameter input not array " ; return ;}  }   function setOrder( $input ){ $this -> Order = $input ;}   function setSearch( $input ){ $this -> Search = $input ;}   function setSort( $input ){ $this -> Sort = $input ;}   function setTables( $input )  {     if ( is_array ( $input )){ $this -> Tables = $input ;}     else { $this -> Error = get_class ( $this ) . " ::setTables Parameter input not array " ; return ;}  }   function setValues( $input )  {     if ( is_array ( $input )){ $this -> Values = $input ;}     else { $this -> Error = get_class ( $this ) . " ::setValues Parameter input not array " ; return ;}  }}?> 

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP基于pdo操作数据库技巧总结》、《PHP运算与运算符用法总结》、《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《php操作office文档技巧总结(包括word,excel,access,ppt)》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

您可能感兴趣的文章:

  • PHP基于单例模式实现的mysql类
  • php封装的连接Mysql类及用法分析
  • 一个php Mysql类 可以参考学习熟悉下
  • 十二个常见的PHP+MySql类免费CMS系统
  • PHP实现基于mysqli的Model基类完整实例
  • PHP使用Mysqli类库实现完美分页效果的方法
  • PHP格式化MYSQL返回float类型的方法
  • php实现Mysql简易操作类
  • php简单操作mysql数据库的类


  • 上一条:
    PHP给文字内容中的关键字进行套红处理
    下一条:
    PHP实现的浏览器检查类
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • 用Time Warden监控PHP中的代码处理时间(0个评论)
    • 在PHP中使用array_pop + yield实现读取超大型目录功能示例(0个评论)
    • Property Hooks RFC在PHP 8.4中越来越接近现实(0个评论)
    • 近期文章
    • 在windows10中升级go版本至1.24后LiteIDE的Ctrl+左击无法跳转问题解决方案(0个评论)
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(95个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-06
    • 2017-07
    • 2017-08
    • 2017-09
    • 2017-11
    • 2017-12
    • 2018-01
    • 2018-02
    • 2018-03
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-09
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-02
    • 2024-03
    • 2024-04
    • 2024-05
    • 2024-06
    • 2024-07
    • 2024-09
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客