php任意数量条件查询函数

2013-6-21 liyangweb.com 李杨 PHP

在做搜索的时候,遇到一个这样的问题,类似于这样的问题,如下图:

有这样一个表单,用户可选的填入"姓名","星座","爱好"表单,点击提交按钮,可以根据用户填入的值自动构造SQL语句,某项不填写,查询时会自动跳过该字段.

 

姓名   星座   爱好   提交

 

之前也遇到过类似的问题,但是处理的方法很麻烦,今天又遇到同样的问题,于是想了一个比较简便的方法,我封装了一个函数,给大家分享一下,这个函数可以根据传入的数据不同,自动的构造合适的SQL语句的where条件.

函数体:

/**
*
*   @array  array{字段=>值,字段=>值}
*
**/
    function where($array) {
        $where='';
        foreach($array as $field=>$value){
            if($value != '' && $where == ''){
                $where=' where '. '`' .$field. '`' .'=\''.$value.'\'';
            }elseif($value != ''){
                $where .="and `{$field}` = '{$value}'";
            }
        }
        return $where;
    }

调用实例1:

    //所有字段均填写
    $array=array(
        'name'=>'李杨',
        'sign'=>'巨蟹',
        'like'=>'编程'
    );
    $sql = 'select * from table'.where($array);
    echo $sql;

打印出的SQL语句为:select * from table where `name`='李杨' and `sign` = '巨蟹' and `like` = '编程'

 

调用实例2:

    //填写部分字段
    $array=array(
        'name'=>'李杨',
        'sign'=>'', //该字段没有填写,接收的值为''
        'like'=>'编程'
    );
    $sql = 'select * from table'.where($array);
    echo $sql;

打印出来的SQL语句为:select * from table where `name`='李杨' and `like` = '编程'

 

该函数只能处理判断条件为"="的SQL,我也找找思路,后续完善一下,如果你有思路的话,欢迎留言.

标签: SQL

评论:

theprintjobs
2019-01-16 04:54
<a href="http://www.lauraundleon.com/game-nike-black-mens-dermontti-dawson-alternate-jersey-nfl-63-pittsburgh-steelers-nflr">game nike black mens dermontti dawson alternate jersey nfl 63 pittsburgh steelers</a> <a href="http://www.michaelphenow.com/adidas-crazylight-boost-femmes-blanc-skoa">adidas crazylight boost femmes blanc</a> <a href="http://www.kguides.com/nike-air-max-95-mens-white-green-sneakere">nike air max 95 mens white green</a> <a href="http://www.nvusforex.com/air-jordan-10-r%C3%B8d-svart-hvit-kurpesa">air jordan 10 r酶d svart hvit</a> <a href="http://www.dinerwaresb.com/nike-hypervenom-phantom-fg-9.5-sneakerb">nike hypervenom phantom fg 9.5</a> <a href="http://www.slipoverfx.com/nike-air-max-womens-outlet-shoesg">nike air max womens outlet</a>
theprintjobs http://www.theprintjobs.com/
033wdp36
2019-01-10 20:02
<a href="http://www.c6wunx94.com/jual-sepatu-nike-free-flyknit-4.0-for-salg-butye">jual sepatu nike free flyknit 4.0 for salg</a> <a href="http://www.6o56ja90.com/kvinders-nike-shox-turbo-21-lilla-lyser%C3%B8d-schuhea">kvinders nike shox turbo 21 lilla lyser酶d</a> <a href="http://www.5eamh55c.com/2015-herre-new-balance-gr%C3%B8n-chaussuresa">2015 herre new balance gr酶n</a> <a href="http://www.o1noys9i.com/herre-nike-blazer-guld-gr%C3%B8n-obuva">herre nike blazer guld gr酶n</a> <a href="http://www.8pfkv589.com/jordan-flight-23-marrone-verde-butyd">jordan flight 23 marrone verde</a> <a href="http://www.37cmfwoq.com/huarache-ultra-breathe-blu-scarpec">huarache ultra breathe blu</a>
033wdp36 http://www.033wdp36.com/
digilowcost
2019-01-03 15:20
<a href="http://www.bontontravel.net/reebok-ventilator-vintage-dg-sort-schuhef">reebok ventilator vintage dg sort</a> <a href="http://www.bowdenagency.net/adidas-pr%C3%B8dator-mania-2014-pro-direct-soccer-scarpee">adidas pr酶dator mania 2014 pro direct soccer</a> <a href="http://www.braceshops.net/nike-flyknit-air-max-australia-for-verkauf-kurpesa">nike flyknit air max australia for verkauf</a> <a href="http://www.briteideaenergy.net/kvinders-nike-air-max-90-vt-lilla-orange-kurpesc">kvinders nike air max 90 vt lilla orange</a> <a href="http://www.brotherfriend.net/kobe-6-varsity-blau-one-obuva">kobe 6 varsity blau one</a> <a href="http://www.brtec.net/dam%C3%A4nner-nike-roshe-run-schwarz-anth-sail-schuhef">dam盲nner nike roshe run schwarz anth sail</a>
digilowcost http://www.digilowcost.net/
steamfestnz
2019-01-02 19:30
<a href="http://www.flatoyota.com/nike-lebron-14-nero-ops-2-obuvf">nike lebron 14 nero ops 2</a> <a href="http://www.fotokompass.com/nike-free-rn-flyknit-noir-performance-obuve">nike free rn flyknit noir performance</a> <a href="http://www.padcrashin.com/flyknit-lunar-2-vendita-uk-a-buon-mercato-kurpesf">flyknit lunar 2 vendita uk a buon mercato</a> <a href="http://www.tickboxit.com/nike-huarache-ultra-mujeres-gris-for-venta-obuvb">nike huarache ultra mujeres gris for venta</a> <a href="http://www.dvrordie.com/femminile-asics-gel-kayano-21-caff%C3%A8-butya">femminile asics gel kayano 21 caff猫</a> <a href="http://www.francisstiff.com/riley-reiff-youth-limited-camo-jersey-nike-nfl-minnesota-vikings-rush-realtree-71-nflu">riley reiff youth limited camo jersey nike nfl minnesota vikings rush realtree 71</a>
steamfestnz http://www.steamfestnz.com/
armedics
2019-01-02 11:35
<a href="http://www.gitmira.com/air-jordan-retro-14-barn-gull-oransje-schuhea">air jordan retro 14 barn gull oransje</a> <a href="http://www.glaceking.com/air-max-97-zebra-bl%C3%A5-zones-kurpese">air max 97 zebra bl氓 zones</a> <a href="http://www.globalsipl.com/adidas-pr%C3%B8dator-pro-kaufen-chaussuresa">adidas pr酶dator pro kaufen</a> <a href="http://www.gofishbaits.com/jordan-3-wolf-gris-srp-kurpesg">jordan 3 wolf gris srp</a> <a href="http://www.golyapoch.com/herren-nike-air-max-2015-wei%C3%9F-schwarz-schuhee">herren nike air max 2015 wei脽 schwarz</a> <a href="http://www.goodbirdsclub.com/nike-free-flyknit-blau-orange-obuvg">nike free flyknit blau orange</a>
armedics http://www.armedics.com/
ochula
2018-12-29 08:56
<a href="http://www.techsrunch.com/nike-blazer-floral-bl%C3%A5-gr%C3%B8nn-butyg">nike blazer floral bl氓 gr酶nn</a> <a href="http://www.tlclease.com/green-bay-packers-david-bakhtiari-official-nike-green-elite-women-s-home-nfl-jersey-nflx">green bay packers david bakhtiari official nike green elite women s home nfl jersey</a> <a href="http://www.beatsbybros.com/youth-nike-buffalo-bills-62-vladimir-ducasse-limited-red-rush-vapor-untouchable-nfl-jersey-nflw">youth nike buffalo bills 62 vladimir ducasse limited red rush vapor untouchable nfl jersey</a> <a href="http://www.dankpixel.com/zx-750-brouge-cord-chaussuresa">zx 750 brouge cord</a> <a href="http://www.givenkface.com/nike-air-max-90-wei%C3%9F-grau-orange-butya">nike air max 90 wei脽 grau orange</a> <a href="http://www.kasabyd.com/nike-mercurial-superfly-v-fg-footbtodas-grass-gren-gris-negro-blanco-butyd">nike mercurial superfly v fg footbtodas grass gren gris negro blanco</a>
ochula http://www.ochula.com/
jazzoutlaw
2018-12-29 02:32
<a href="http://www.bladeid.com/nike-kyrie-1-dark-gr%C3%A5-pants-scarpef">nike kyrie 1 dark gr氓 pants</a> <a href="http://www.bakomoms.com/air-jordan-13-venta-kit-kurpesa">air jordan 13 venta kit</a> <a href="http://www.macrackmount.com/nike-flyknit-lunar-2-l%C3%B8b-sko-ho14-schuhed">nike flyknit lunar 2 l酶b sko ho14</a> <a href="http://www.martinsedacca.com/nike-free-5.0-grau-blau-kurpesb">nike free 5.0 grau blau</a> <a href="http://www.maxibymaxi.com/air-jordan-14-retro-sort-hvid-kurpesa">air jordan 14 retro sort hvid</a> <a href="http://www.slatervision.com/adidas-porsche-typ-64-noir-orange-obuvc">adidas porsche typ 64 noir orange</a>
[url=http://www.jazzoutlaw.com/]jazzoutlaw[/url]
fabloomsilk
2018-12-27 13:40
<a href="http://www.lauraundleon.com/game-nike-black-mens-dermontti-dawson-alternate-jersey-nfl-63-pittsburgh-steelers-nflr">game nike black mens dermontti dawson alternate jersey nfl 63 pittsburgh steelers</a> <a href="http://www.michaelphenow.com/adidas-crazylight-boost-femmes-blanc-skoa">adidas crazylight boost femmes blanc</a> <a href="http://www.kguides.com/nike-air-max-95-mens-white-green-sneakere">nike air max 95 mens white green</a> <a href="http://www.nvusforex.com/air-jordan-10-r%C3%B8d-svart-hvit-kurpesa">air jordan 10 r酶d svart hvit</a> <a href="http://www.dinerwaresb.com/nike-hypervenom-phantom-fg-9.5-sneakerb">nike hypervenom phantom fg 9.5</a> <a href="http://www.slipoverfx.com/nike-air-max-womens-outlet-shoesg">nike air max womens outlet</a>
fabloomsilk http://www.fabloomsilk.com/

发表评论:

Powered by emlog 冀ICP备13011830号-1