zoukankan      html  css  js  c++  java
  • PHP多条件模糊查询

    所使用的方法:$sqlArr=array();array_push();implode();

    原理,

    一、建立sql语句前半句,并且建立一个空数组。

    二、根据条件是否为空来判断是否向数组中添加元素。如果不为空,使用array_push()方法来添加,第一个参数为数组名称,第二个参数为值。

    三、全部条件判断完毕用implode()方法来拆分数组。第一个参数为使用什么字符来拆分,可以为字符串,第二个参数为数组。

    四、加上sql语句后半句。完成sql语句!

    例如:

    $sql="select * from member where member_Type=0 and (";
      $sqlArr=array();
      if($member_id!="")
       array_push($sqlArr," id like '$member_id' ");
      if($member_Name!="")
       array_push($sqlArr," member_Name like '%$member_Name%' ");
      if($member_Creation!="")
       array_push($sqlArr," member_Creation>'%$member_Creation%'");
      $sql.=implode(" or ",$sqlArr);
      $sql.=") order by id desc";$rs=mysql_query($sql);
     $total=mysql_num_rows($rs);
     $totalpage=ceil($total/$PageSize);
     if($page>$totalpage)
      $page=$totalpage;

    $sql="select * from member where member_Type=0 and (";
      $sqlArr=array();
      if($member_id!="")
       array_push($sqlArr," id like '$member_id' ");
      if($member_Name!="")
       array_push($sqlArr," member_Name like '%$member_Name%' ");
      if($member_Creation!="")
       array_push($sqlArr," member_Creation>'%$member_Creation%'");
      $sql.=implode(" or ",$sqlArr);
      $sql.=") order by id desc limit ".($page-1)*$PageSize.",".$PageSize;

    这样,所有的参数都可以添加进来。

  • 相关阅读:
    Winform—C#读写config配置文件
    C# 中Web.config文件的读取与写入
    Redis配置文件详解
    三层架构之泛型抽象
    Linq To Sql语法及实例大全
    junit单元测试(keeps the bar green to keeps the code clean)
    观 GT Java语言管理系统的感悟
    java考核完的心得
    15个C++项目列表
    C++文件操作(fstream)
  • 原文地址:https://www.cnblogs.com/snowhite/p/7048676.html
Copyright © 2011-2022 走看看