亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 語言 > PHP > 正文

PHP實現的通過參數生成MYSQL語句類完整實例

2024-05-04 23:44:58
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了PHP實現的通過參數生成MYSQL語句類,結合完整實例形式分析了生成MYSQL語句類的實現與使用技巧,需要的朋友可以參考下
 

本文實例講述了PHP實現的通過參數生成MYSQL語句類。分享給大家供大家參考,具體如下:

這個類可以通過指定的表和字段參數創建SELECT ,INSERT , UPDATE 和 DELETE 語句。

這個類可以創建SQL語句的WHERE條件,像LIKE的查詢語句,使用LEFT JOIN和ORDER 語句

<?php /* *******************************************************************Example fileThis example shows how to use the MyLibSQLGen classThe example is based on the following MySQL table:CREATE TABLE customer ( id int(10) unsigned NOT NULL auto_increment, name varchar(60) NOT NULL default '', address varchar(60) NOT NULL default '', city varchar(60) NOT NULL default '', PRIMARY KEY (cust_id)) TYPE=MyISAM;******************************************************************* */  require_once ( " class_mylib_SQLGen-1.0.php " ); $fields = Array ( " name " , " address " , " city " ); $values = Array ( " Fadjar " , " Resultmang Raya Street " , " Jakarta " ); $tables = Array ( " customer " ); echo  " <b>Result Generate Insert</b><br> " ; $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 . " <br> " ;} echo  " <b>Result Generate Update</b><br> " ; $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 . " <br> " ;} echo  " <b>Result Generate Delete</b><br> " ; $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 . " <br> " ;} echo  " <b>Result Generate List</b><br> " ; $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 . " <br> " ;} echo  " <b>Result Generate List with search on all fields</b><br> " ; $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 . " <br> " ;} echo  " <b>Result Generate List with search on some fields</b><br> " ; $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 . " <br> " ;}?> 

類代碼:

<?php /* Created By    : Fadjar Nurswanto <fajr_n@rindudendam.net>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教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
www国产91| 91免费精品国偷自产在线| 丝袜亚洲欧美日韩综合| 国产91精品不卡视频| 成人激情视频小说免费下载| 午夜精品一区二区三区在线视频| 精品久久久久久久中文字幕| 影音先锋欧美在线资源| 亚洲最大福利视频网站| 狠狠躁天天躁日日躁欧美| 欧美视频国产精品| 成人国产精品色哟哟| 国产视频综合在线| 亚洲免费小视频| 久久精品国产免费观看| 91美女高潮出水| 日av在线播放中文不卡| 欧美日韩国产中文字幕| 成人高清视频观看www| 青青精品视频播放| 亚洲最新av网址| 51ⅴ精品国产91久久久久久| 中文字幕久久久| 中文字幕亚洲欧美在线| 欧美壮男野外gaytube| 77777少妇光屁股久久一区| 色中色综合影院手机版在线观看| 中文字幕精品av| 欧美日韩第一页| 中文字幕精品在线| 亚洲精品视频在线播放| 国产91网红主播在线观看| 欧美激情一级二级| 欧美成人激情图片网| 亚洲美女av在线播放| 日韩精品视频在线免费观看| 国产成人aa精品一区在线播放| 欧美激情亚洲激情| 国产亚洲欧美日韩精品| 国产精品一区二区久久久| 欧美精品激情blacked18| 国产日韩欧美日韩| 91国内在线视频| **欧美日韩vr在线| 亚洲一区二区三区四区在线播放| 夜夜狂射影院欧美极品| 91在线播放国产| 国产精品丝袜久久久久久高清| 久久久久久成人精品| 九九精品视频在线| 日韩电影免费观看在线| 97视频免费在线观看| 欧美性69xxxx肥| 91精品国产91久久久久福利| 国产福利视频一区二区| 中文字幕少妇一区二区三区| 久久在线精品视频| 欧美一级黑人aaaaaaa做受| 国产91成人video| 亚洲人成网站999久久久综合| 日本中文字幕久久看| 亚洲中国色老太| 成人乱人伦精品视频在线观看| 青草青草久热精品视频在线观看| 色偷偷偷综合中文字幕;dd| 亚洲人成电影网站| 欧美大全免费观看电视剧大泉洋| 欧美电影免费观看| 欧美不卡视频一区发布| 中文字幕亚洲情99在线| 国产91免费看片| 国产日韩精品在线观看| 国产精品劲爆视频| 亚洲精品电影网在线观看| 91久久精品国产91久久| 最近的2019中文字幕免费一页| 欧美精品在线看| 亚洲精品成人免费| 精品久久久久久久久久久| 国产亚洲在线播放| 午夜精品久久久久久久99热浪潮| 亚洲第一网站男人都懂| 国产精品青青在线观看爽香蕉| 91夜夜揉人人捏人人添红杏| 欧美日韩国产精品一区二区不卡中文| 国产精品香蕉av| 日韩精品在线免费观看| 日韩欧美国产免费播放| 欧美国产亚洲精品久久久8v| 少妇久久久久久| 欧美日本啪啪无遮挡网站| 一区二区亚洲欧洲国产日韩| 成人免费视频97| 欧美亚洲日本黄色| 国产欧美日韩精品专区| 97国产精品人人爽人人做| 日韩免费观看在线观看| 亚洲天天在线日亚洲洲精| 亚洲黄色免费三级| 久久夜色精品国产| 91精品国产综合久久久久久蜜臀| 国产亚洲综合久久| 国产精品视频不卡| 亚洲www永久成人夜色| 欧美日韩亚洲视频一区| 中文日韩在线观看| 亚洲精品美女久久| 国产免费亚洲高清| 亚洲一区二区福利| 国产成人一区二区三区小说| 国产不卡在线观看| 久久人人爽人人爽人人片av高清| 国产99视频在线观看| 国产精品中文字幕在线| 久久精品国产亚洲一区二区| 日韩在线欧美在线国产在线| 日本一本a高清免费不卡| 91久久久久久久久久久| 久久精品国产v日韩v亚洲| 国产精品视频一区二区三区四| 国产视频综合在线| 国产精品激情av电影在线观看| 亚洲a成v人在线观看| 国产精品国产三级国产aⅴ浪潮| 国产高清视频一区三区| 日韩免费电影在线观看| 精品无人区乱码1区2区3区在线| 久久视频在线直播| 亚洲国产精品va在看黑人| 综合久久五月天| 日韩欧美黄色动漫| 久久精品电影网站| 日本午夜在线亚洲.国产| 国产精品女主播视频| 久久91精品国产91久久跳| 日本成人精品在线| 精品亚洲一区二区三区在线观看| 热久久视久久精品18亚洲精品| 国产97在线亚洲| 日本久久久久久久| 日韩在线免费av| 亚洲精品视频免费| 亚洲第一精品电影| 欧美国产日产韩国视频| 亚洲一区免费网站| 色婷婷综合久久久久中文字幕1| 国产在线视频不卡| 欧美亚洲成人精品| 欧美激情2020午夜免费观看| 欧美黄色免费网站| 亚洲男人天堂九九视频| 久久成人精品一区二区三区| 91精品国产自产在线观看永久| 欧美精品久久久久久久| 亚洲色图校园春色| 91在线国产电影| 国产亚洲精品日韩| 国产成人综合精品在线| 免费av一区二区| 亚洲高清免费观看高清完整版| 麻豆国产va免费精品高清在线| 4444欧美成人kkkk| 国产视频精品久久久| 国产精品免费久久久|