查看: 4775|回复: 0
打印 上一主题 下一主题

动态网页技术PHP通过参数来生成MYSQL语句类

[复制链接]
跳转到指定楼层
1#
发表于 2007-10-5 15:40:50 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
台州网址导航
这个类可以通过具有参数的数组来构建MySQL查询语句。
  这个类可以通过指定的表和字段参数创建SELECT ,INSERT , UPDATE 和 DELETE 语句。
  这个类可以创建SQL语句的WHERE条件,像LIKE的查询语句,使用LEFT JOIN和ORDER 语句。
  例子:
  1.    <? php
  2.    /* *******************************************************************
  3.   Example file
  4.   This example shows how to use the MyLibSQLGen class
  5.    
  6.   The example is based on the following MySQL table:
  7.    
  8.   CREATE TABLE customer (
  9.    id int(10) unsigned NOT NULL auto_increment,
  10.    name varchar(60) NOT NULL default '',
  11.    address varchar(60) NOT NULL default '',
  12.    city varchar(60) NOT NULL default '',
  13.    PRIMARY KEY (cust_id)
  14.   ) TYPE=MyISAM;
  15.    
  16.   ******************************************************************* */
  17.    
  18.    require_once ( " class_mylib_SQLGen-1.0.php " );
  19.    
  20.    $fields = Array ( " name " , " address " , " city " );
  21.    $values = Array ( " Fadjar " , " Resultmang Raya Street " , " Jakarta " );
  22.    $tables = Array ( " customer " );
  23.    
  24.    echo " Result Generate Insert
  25. " ;
  26.    $object = new MyLibSQLGen();
  27.    $object -> clear_all_assign(); // to refresh all property but it no need when first time execute
  28.    $object -> setFields( $fields );
  29.    $object -> setValues( $values );
  30.    $object -> setTables( $tables );
  31.    
  32.    if ( ! $object -> getInsertSQL()){ echo $object -> Error; exit ;}
  33.    else { $sql = $object -> Result; echo $sql . "
  34. " ;}
  35.    
  36.    
  37.    echo " Result Generate Update
  38. " ;
  39.    $fields = Array ( " name " , " address " , " city " );
  40.    $values = Array ( " Fadjar " , " Resultmang Raya Street " , " Jakarta " );
  41.    $tables = Array ( " customer " );
  42.    $id = 1 ;
  43.    $conditions [ 0 ][ " condition " ] = " id='$id' " ;
  44.    $conditions [ 0 ][ " connection " ] = "" ;
  45.    
  46.    $object -> clear_all_assign();
  47.    $object -> setFields( $fields );
  48.    $object -> setValues( $values );
  49.    $object -> setTables( $tables );
  50.    $object -> setConditions( $conditions );
  51.    
  52.    if ( ! $object -> getUpdateSQL()){ echo $object -> Error; exit ;}
  53.    else { $sql = $object -> Result; echo $sql . "
  54. " ;}
  55.    
  56.    echo " Result Generate Delete
  57. " ;
  58.    $tables = Array ( " customer " );
  59.    $conditions [ 0 ][ " condition " ] = " id='1' " ;
  60.    $conditions [ 0 ][ " connection " ] = " OR " ;
  61.    $conditions [ 1 ][ " condition " ] = " id='2' " ;
  62.    $conditions [ 1 ][ " connection " ] = " OR " ;
  63.    $conditions [ 2 ][ " condition " ] = " id='4' " ;
  64.    $conditions [ 2 ][ " connection " ] = "" ;
  65.    
  66.    $object -> clear_all_assign();
  67.    $object -> setTables( $tables );
  68.    $object -> setConditions( $conditions );
  69.    
  70.    if ( ! $object -> getDeleteSQL()){ echo $object -> Error; exit ;}
  71.    else { $sql = $object -> Result; echo $sql . "
  72. " ;}
  73.    
  74.    echo " Result Generate List
  75. " ;
  76.    $fields = Array ( " id " , " name " , " address " , " city " );
  77.    $tables = Array ( " customer " );
  78.    $id = 1 ;
  79.    $conditions [ 0 ][ " condition " ] = " id='$id' " ;
  80.    $conditions [ 0 ][ " connection " ] = "" ;
  81.    
  82.    $object -> clear_all_assign();
  83.    $object -> setFields( $fields );
  84.    $object -> setTables( $tables );
  85.    $object -> setConditions( $conditions );
  86.    
  87.    if ( ! $object -> getQuerySQL()){ echo $object -> Error; exit ;}
  88.    else { $sql = $object -> Result; echo $sql . "
  89. " ;}
  90.    
  91.    echo " Result Generate List with search on all fields
  92. " ;
  93.    $fields = Array ( " id " , " name " , " address " , " city " );
  94.    $tables = Array ( " customer " );
  95.    $id = 1 ;
  96.    $search = " Fadjar Nurswanto " ;
  97.    $object -> clear_all_assign();
  98.    $object -> setFields( $fields );
  99.    $object -> setTables( $tables );
  100.    $object -> setSearch( $search );
  101.    
  102.    if ( ! $object -> getQuerySQL()){ echo $object -> Error; exit ;}
  103.    else { $sql = $object -> Result; echo $sql . "
  104. " ;}
  105.    
  106.    echo " Result Generate List with search on some fields
  107. " ;
  108.    $fields = Array ( " id " , " name " , " address " , " city " );
  109.    $tables = Array ( " customer " );
  110.    $id = 1 ;
  111.    $search = Array (
  112.    " name " => " Fadjar Nurswanto " ,
  113.    " address " => " Tomang Raya "
  114.    );
  115.    
  116.    $object -> clear_all_assign();
  117.    $object -> setFields( $fields );
  118.    $object -> setTables( $tables );
  119.    $object -> setSearch( $search );
  120.    
  121.    if ( ! $object -> getQuerySQL()){ echo $object -> Error; exit ;}
  122.    else { $sql = $object -> Result; echo $sql . "
  123. " ;}
  124.    ?>
  125.    
  126.   类代码:
  127.    <? php
  128.    /*
  129.   Created By : Fadjar Nurswanto  
  130.   DATE : 2006-08-02
  131.   PRODUCTNAME : class MyLibSQLGen
  132.   PRODUCTVERSION : 1.0.0
  133.   DESCRIPTION : class yang berfungsi untuk menggenerate SQL
  134.   DENPENCIES :
  135.    */
  136.    class MyLibSQLGen
  137.   {
  138.    var $Result ;
  139.    var $Tables = Array ();
  140.    var $Values = Array ();
  141.    var $Fields = Array ();
  142.    var $Conditions = Array ();
  143.    var $Condition ;
  144.    var $LeftJoin = Array ();
  145.    var $Search ;
  146.    var $Sort = " ASC " ;
  147.    var $Order ;
  148.    var $Error ;
  149.    
  150.    function MyLibSQLGen(){}
  151.    function BuildCondition()
  152.    {
  153.    $funct = " BuildCondition " ;
  154.    $className = get_class ( $this );
  155.    $conditions = $this -> getConditions();
  156.    if ( ! $conditions ){ $this -> dbgDone( $funct ); return true ;}
  157.    if ( ! is_array ( $conditions ))
  158.    {
  159.    $this -> Error = " $className::$funct \nVariable conditions not Array " ;
  160.    return ;
  161.    }
  162.    for ( $i = 0 ; $i < count ( $conditions ); $i ++ )
  163.    {
  164.    $this -> Condition .= $conditions [ $i ][ " condition " ] . " " . $conditions [ $i ][ " connection " ] . " " ;
  165.    }
  166.    return true ;
  167.    }
  168.    function BuildLeftJoin()
  169.    {
  170.    $funct = " BuildLeftJoin " ;
  171.    $className = get_class ( $this );
  172.    if ( ! $this -> getLeftJoin()){ $this -> Error = " $className::$funct \nProperty LeftJoin was empty " ; return ;}
  173.    
  174.    $LeftJoinVars = $this -> getLeftJoin();
  175.    
  176.    $hasil = false ;
  177.    foreach ( $LeftJoinVars as $LeftJoinVar )
  178.    {
  179.    @ $hasil .= " LEFT JOIN " . $LeftJoinVar [ " table " ];
  180.    foreach ( $LeftJoinVar [ " on " ] as $var )
  181.    {
  182.    @ $condvar .= $var [ " condition " ] . " " . $var [ " connection " ] . " " ;
  183.    }
  184.    $hasil .= " ON ( " . $condvar . " ) " ;
  185.    unset ( $condvar );
  186.    }
  187.    
  188.    $this -> ResultLeftJoin = $hasil ;
  189.    
  190.    return true ;
  191.    }
  192.    function BuildOrder()
  193.    {
  194.    $funct = " BuildOrder " ;
  195.    $className = get_class ( $this );
  196.    if ( ! $this -> getOrder()){ $this -> Error = " $className::$funct \nProperty Order was empty " ; return ;}
  197.    if ( ! $this -> getFields()){ $this -> Error = " $className::$funct \nProperty Fields was empty " ; return ;}
  198.    
  199.    $Fields = $this -> getFields();
  200.    $Orders = $this -> getOrder();
  201.    if ( ereg ( " , " , $Orders )){ $Orders = explode ( " , " , $Order );}
  202.    if ( ! is_array ( $Orders )){ $Orders = Array ( $Orders );}
  203.    
  204.    foreach ( $Orders as $Order )
  205.    {
  206.    if ( ! is_numeric ( $Order )){ $this -> Error = " $className::$funct \nProperty Order not Numeric " ; return ;}
  207.    if ( $Order > count ( $this -> Fields)){ $this -> Error = " $className::$funct \nMax value of property Sort is " . count ( $this -> Fields); return ;}
  208.    
  209.    @ $xorder .= $Fields [ $Order ] . " , " ;
  210.    }
  211.    
  212.    $this -> ResultOrder = " ORDER BY " . substr ( $xorder , 0 ,- 1 );
  213.    
  214.    return true ;
  215.    }
  216.    function BuildSearch()
  217.    {
  218.    $funct = " BuildSearch " ;
  219.    $className = get_class ( $this );
  220.    
  221.    if ( ! $this -> getSearch()){ $this -> Error = " $className::$funct \nProperty Search was empty " ; return ;}
  222.    if ( ! $this -> getFields()){ $this -> Error = " $className::$funct \nProperty Fields was empty " ; return ;}
  223.    
  224.    $Fields = $this -> getFields();
  225.    $xvalue = $this -> getSearch();
  226.    
  227.    if ( is_array ( $xvalue ))
  228.    {
  229.    foreach ( $Fields as $field )
  230.    {
  231.    if (@ $xvalue [ $field ])
  232.    {
  233.    $Values = explode ( " " , $xvalue [ $field ]);
  234.    foreach ( $Values as $Value )
  235.    {
  236.    @ $hasil .= $field . " LIKE '% " . $Value . " %' OR " ;
  237.    }
  238.    if ( $hasil )
  239.    {
  240.    @ $hasil_final .= " ( " . substr ( $hasil , 0 ,- 4 ) . " ) AND " ;
  241.    unset ( $hasil );
  242.    }
  243.    }
  244.    }
  245.    $hasil = $hasil_final ;
  246.    }
  247.    else
  248.    {
  249.    foreach ( $Fields as $field )
  250.    {
  251.    $Values = explode ( " " , $xvalue );
  252.    foreach ( $Values as $Value )
  253.    {
  254.    @ $hasil .= $field . " LIKE '% " . $Value . " %' OR " ;
  255.    }
  256.    }
  257.    }
  258.    
  259.    $this -> ResultSearch = substr ( $hasil , 0 ,- 4 );
  260.    return true ;
  261.    }
  262.    function clear_all_assign()
  263.    {
  264.    $this -> Result = null ;
  265.    $this -> ResultSearch = null ;
  266.    $this -> ResultLeftJoin = null ;
  267.    $this -> Result = null ;
  268.    $this -> Tables = Array ();
  269.    $this -> Values = Array ();
  270.    $this -> Fields = Array ();
  271.    $this -> Conditions = Array ();
  272.    $this -> Condition = null ;
  273.    $this -> LeftJoin = Array ();
  274.    $this -> Sort = " ASC " ;
  275.    $this -> Order = null ;
  276.    $this -> Search = null ;
  277.    $this -> fieldSQL = null ;
  278.    $this -> valueSQL = null ;
  279.    $this -> partSQL = null ;
  280.    $this -> Error = null ;
  281.    return true ;
  282.    }
  283.    function CombineFieldValue( $manual = false )
  284.    {
  285.    $funct = " CombineFieldsPostVar " ;
  286.    $className = get_class ( $this );
  287.    $fields = $this -> getFields();
  288.    $values = $this -> getValues();
  289.    if ( ! is_array ( $fields ))
  290.    {
  291.    $this -> Error = " $className::$funct \nVariable fields not Array " ;
  292.    return ;
  293.    }
  294.    if ( ! is_array ( $values ))
  295.    {
  296.    $this -> Error = " $className::$funct \nVariable values not Array " ;
  297.    return ;
  298.    }
  299.    if ( count ( $fields ) != count ( $values ))
  300.    {
  301.    $this -> Error = " $className::$funct \nCount of fields and values not match " ;
  302.    return ;
  303.    }
  304.    for ( $i = 0 ; $i < count ( $fields ); $i ++ )
  305.    {
  306.    @ $this -> fieldSQL .= $fields [ $i ] . " , " ;
  307.    if ( $fields [ $i ] == " pwd " || $fields [ $i ] == " password " || $fields [ $i ] == " pwd " )
  308.    {
  309.    @ $this -> valueSQL .= " password(' " . $values [ $i ] . " '), " ;
  310.    @ $this -> partSQL .= $fields [ $i ] . " =password(' " . $values [ $i ] . " '), " ;
  311.    }
  312.    else
  313.    {
  314.    if ( is_numeric ( $values [ $i ]))
  315.    {
  316.    @ $this -> valueSQL .= $values [ $i ] . " , " ;
  317.    @ $this -> partSQL .= $fields [ $i ] . " = " . $values [ $i ] . " , " ;
  318.    }
  319.    else
  320.    {
  321.    @ $this -> valueSQL .= " ' " . $values [ $i ] . " ', " ;
  322.    @ $this -> partSQL .= $fields [ $i ] . " =' " . $values [ $i ] . " ', " ;
  323.    }
  324.    }
  325.    }
  326.    $this -> fieldSQL = substr ( $this -> fieldSQL , 0 ,- 1 );
  327.    $this -> valueSQL = substr ( $this -> valueSQL , 0 ,- 1 );
  328.    $this -> partSQL = substr ( $this -> partSQL , 0 ,- 1 );
  329.    return true ;
  330.    }
  331.    function getDeleteSQL()
  332.    {
  333.    $funct = " getDeleteSQL " ;
  334.    $className = get_class ( $this );
  335.    $Tables = $this -> getTables();
  336.    if ( ! $Tables || ! count ( $Tables ))
  337.    {
  338.    $this -> dbgFailed( $funct );
  339.    $this -> Error = " $className::$funct \nTable was empty " ;
  340.    return ;
  341.    }
  342.    for ( $i = 0 ; $i < count ( $Tables ); $i ++ )
  343.    {
  344.    @ $Table .= $Tables [ $i ] . " , " ;
  345.    }
  346.    $Table = substr ( $Table , 0 ,- 1 );
  347.    
  348.    $sql = " DELETE FROM " . $Table ;
  349.    
  350.    if ( $this -> getConditions())
  351.    {
  352.    if ( ! $this -> BuildCondition()){ $this -> dbgFailed( $funct ); return ;}
  353.    $sql .= " WHERE " . $this -> getCondition();
  354.    }
  355.    $this -> Result = $sql ;
  356.    return true ;
  357.    }
  358.    function getInsertSQL()
  359.    {
  360.    $funct = " getInsertSQL " ;
  361.    $className = get_class ( $this );
  362.    if ( ! $this -> getValues()){ $this -> Error = " $className::$funct \nProperty Values was empty " ; return ;}
  363.    if ( ! $this -> getFields()){ $this -> Error = " $className::$funct \nProperty Fields was empty " ; return ;}
  364.    if ( ! $this -> getTables()){ $this -> Error = " $className::$funct \nProperty Tables was empty " ; return ;}
  365.    
  366.    if ( ! $this -> CombineFieldValue()){ $this -> dbgFailed( $funct ); return ;}
  367.    $Tables = $this -> getTables();
  368.    
  369.    $sql = " INSERT INTO " . $Tables [ 0 ] . " ( " . $this -> fieldSQL . " ) VALUES ( " . $this -> valueSQL . " ) " ;
  370.    
  371.    $this -> Result = $sql ;
  372.    
  373.    return true ;
  374.    }
  375.    function getUpdateSQL()
  376.    {
  377.    $funct = " getUpdateSQL " ;
  378.    $className = get_class ( $this );
  379.    
  380.    if ( ! $this -> getValues()){ $this -> Error = " $className::$funct \nProperty Values was empty " ; return ;}
  381.    if ( ! $this -> getFields()){ $this -> Error = " $className::$funct \nProperty Fields was empty " ; return ;}
  382.    if ( ! $this -> getTables()){ $this -> Error = " $className::$funct \nProperty Tables was empty " ; return ;}
  383.    
  384.    if ( ! $this -> CombineFieldValue()){ $this -> dbgFailed( $funct ); return ;}
  385.    if ( ! $this -> BuildCondition()){ $this -> dbgFailed( $funct ); return ;}
  386.    $Tables = $this -> getTables();
  387.    
  388.    $sql = " UPDATE " . $Tables [ 0 ] . " SET " . $this -> partSQL . " WHERE " . $this -> getCondition();
  389.    
  390.    $this -> Result = $sql ;
  391.    
  392.    return true ;
  393.    }
  394.    function getQuerySQL()
  395.    {
  396.    $funct = " getQuerySQL " ;
  397.    $className = get_class ( $this );
  398.    
  399.    if ( ! $this -> getFields()){ $this -> Error = " $className::$funct \nProperty Fields was empty " ; return ;}
  400.    if ( ! $this -> getTables()){ $this -> Error = " $className::$funct \nProperty Tables was empty " ; return ;}
  401.    
  402.    $Fields = $this -> getFields();
  403.    $Tables = $this -> getTables();
  404.    foreach ( $Fields as $Field ){@ $sql_raw .= $Field . " , " ;}
  405.    foreach ( $Tables as $Table ){@ $sql_table .= $Table . " , " ;}
  406.    
  407.    $this -> Result = " SELECT " . substr ( $sql_raw , 0 ,- 1 ) . " FROM " . substr ( $sql_table , 0 ,- 1 );
  408.    
  409.    if ( $this -> getLeftJoin())
  410.    {
  411.    if ( ! $this -> BuildLeftJoins()){ $this -> dbgFailed( $funct ); return ;}
  412.    $this -> Result .= " " . $this -> ResultLeftJoin;
  413.    }
  414.    if ( $this -> getConditions())
  415.    {
  416.    if ( ! $this -> BuildCondition()){ $this -> dbgFailed( $funct ); return ;}
  417.    $this -> Result .= " WHERE ( " . $this -> Condition . " ) " ;
  418.    }
  419.    if ( $this -> getSearch())
  420.    {
  421.    if ( ! $this -> BuildSearch()){ $this -> dbgFailed( $funct ); return ;}
  422.    if ( $this -> ResultSearch)
  423.    {
  424.    if ( eregi ( " WHERE " , $this -> Result)){ $this -> Result .= " AND " . $this -> ResultSearch;}
  425.    else { $this -> Result .= " WHERE " . $this -> ResultSearch;}
  426.    }
  427.    }
  428.    if ( $this -> getOrder())
  429.    {
  430.    if ( ! $this -> BuildOrder()){ $this -> dbgFailed( $funct ); return ;}
  431.    $this -> Result .= " " . $this -> ResultOrder;
  432.    }
  433.    if ( $this -> getSort())
  434.    {
  435.    if (@ $this -> ResultOrder)
  436.    {
  437.    $this -> Result .= " " . $this -> getSort();
  438.    }
  439.    }
  440.    
  441.    return true ;
  442.    }
  443.    
  444.    function getCondition(){ return @ $this -> Condition;}
  445.    function getConditions(){ if ( count (@ $this -> Conditions) && is_array (@ $this -> Conditions)){ return @ $this -> Conditions;}}
  446.    function getFields(){ if ( count (@ $this -> Fields) && is_array (@ $this -> Fields)){ return @ $this -> Fields;}}
  447.    function getLeftJoin(){ if ( count (@ $this -> LeftJoin) && is_array (@ $this -> LeftJoin)){ return @ $this -> LeftJoin;}}
  448.    function getOrder(){ return @ $this -> Order;}
  449.    function getSearch(){ return @ $this -> Search;}
  450.    function getSort(){ return @ $this -> Sort ;}
  451.    function getTables(){ if ( count (@ $this -> Tables) && is_array (@ $this -> Tables)){ return @ $this -> Tables;}}
  452.    function getValues(){ if ( count (@ $this -> Values) && is_array (@ $this -> Values)){ return @ $this -> Values;}}
  453.    
  454.    function setCondition( $input ){ $this -> Condition = $input ;}
  455.    function setConditions( $input )
  456.    {
  457.    if ( is_array ( $input )){ $this -> Conditions = $input ;}
  458.    else { $this -> Error = get_class ( $this ) . " ::setConditions \nParameter input not array " ; return ;}
  459.    }
  460.    function setFields( $input )
  461.    {
  462.    if ( is_array ( $input )){ $this -> Fields = $input ;}
  463.    else { $this -> Error = get_class ( $this ) . " ::setFields \nParameter input not array " ; return ;}
  464.    }
  465.    function setLeftJoin( $input )
  466.    {
  467.    if ( is_array ( $input )){ $this -> LeftJoin = $input ;}
  468.    else { $this -> Error = get_class ( $this ) . " ::setFields \nParameter input not array " ; return ;}
  469.    }
  470.    function setOrder( $input ){ $this -> Order = $input ;}
  471.    function setSearch( $input ){ $this -> Search = $input ;}
  472.    function setSort( $input ){ $this -> Sort = $input ;}
  473.    function setTables( $input )
  474.    {
  475.    if ( is_array ( $input )){ $this -> Tables = $input ;}
  476.    else { $this -> Error = get_class ( $this ) . " ::setTables \nParameter input not array " ; return ;}
  477.    }
  478.    function setValues( $input )
  479.    {
  480.    if ( is_array ( $input )){ $this -> Values = $input ;}
  481.    else { $this -> Error = get_class ( $this ) . " ::setValues \nParameter input not array " ; return ;}
  482.    }
  483.   }
  484.    ?>
复制代码
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 转播转播 分享分享 分享淘帖
台州维博网络(www.tzweb.com)专门运用PHP+MYSQL/ASP.NET+MSSQL技术开发网站门户平台系统等。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

网站推广
关于我们
  • 台州朗动科技(Tzweb.com)拥有多年开发网站平台系统门户手机客户端等业务的成功经验。主要从事:政企网站,系统平台,微信公众号,各类小程序,手机APP客户端,浙里办微应用,浙政钉微应用、主机域名、虚拟空间、后期维护等服务,满足不同企业公司的需求,是台州地区领先的网络技术服务商!

Hi,扫描关注我

Copyright © 2005-2026 站长论坛 All rights reserved

Powered by 站长论坛 with TZWEB Update Techonolgy Support

快速回复 返回顶部 返回列表