多个关键字搜索使用php mysql结果相关记录

时间:2015-05-09 07:18:58

标签: php mysql

ID    CODE      STATE    CITY            AREA
1   SBIN0000952 ORISSA  JAIPUR          TOWN
2   SBIN0000953 ORISSA  KURAPUT         VILLAGE
3   SBIN0000954 DELHI   DELHI           TOWN
4   SBIN0000955 DELHI   NEW DELHI       VILLAGE
5   SBIN0000956 GOA     SOUTH GOA       VILLAGE
6   SBIN0000957 GOA     PANAJI          TOWN
7   SBIN0000958 KERLA   CHOCHIN         TOWN
8   SBIN0000959 KERLA   TRIVANDRAM      VILLAGE
9   SBIN0000960 ANDHRA  VIZAG           TOWN
10  SBIN0000961 ANDHRA  HYDERABAD       VILLAGE

上面是我的表格,我希望搜索多个关键字,例如

然后它应该显示记录

7   SBIN0000958 KERLA   CHOCHIN    TOWN

低于我目前在php中的代码

include('dbConnect.inc.php');

//collect
if(!isset($_POST['search'])){
    header("Location:index.php");

} 
    $searchq = mysql_real_escape_string( $_POST['search']);
    $search_sql = "SELECT * FROM `bankifscin` WHERE STATE LIKE '%$searchq%' OR CITY LIKE '%$searchq%' OR CODE LIKE '%$searchq%' ";
    $search_query = mysql_query($search_sql) or die(mysql_error());
    if(mysql_num_rows($search_query)!=0){
    $search_rs = mysql_fetch_assoc($search_query);
    }

打印

<?php if(mysql_num_rows($search_query)!=0){
    do{ ?>

    <?php 

    $bank = $search_rs['BANK'];
    $ifsc = $search_rs['IFSC'];
    $branch= $search_rs['BRANCH'];
    $micr = $search_rs['MICR_CODE'];
    $address = $search_rs['ADDRESS'];
    $contact = $search_rs['CONTACT'];
    $city = $search_rs['CITY'];
    $district = $search_rs['DISTRICT'];
    $state = $search_rs['STATE'];
         ?>
         <table width="100%" border="2" bordercolor="#000" class="bdrcolor">


    <tbody>     
  <tr>
    <td width="15%"><?=$bank?></td> 
       <td width="15%"><?=$branch?></td>
       <td width="15%"><b>IFSC:</b><?=$ifsc?> <br /><b>MICR:</b><?=$micr?></td>
       <td width="20%"><?=$address?><br /> <b>City :</b><?=$city?> <br /> <b>District :</b><?=$district?> <br /> <b>State:</b> <?=$state?></td> 
       <td width="10%"><?=$contact?></td>
  </tr>
  <br />
  </tbody>
</table>



    <?php   }while ($search_rs = mysql_fetch_assoc($search_query)); 

}else{
        echo "No Results";

        }


    ?>

它只给出一个关键字的结果或没有结果

4 个答案:

答案 0 :(得分:0)

您应该迭代结果集,以查看所有记录。

    $result = mysql_query($query);
    if ($result != NULL)
    {
      while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
      {
        foreach ($row as $key => $value)
        {
          // todo: use $key and $value
        }
      }
      mysql_free_result($result);
   }

修改

如果您想知道,请致电mysql_fetch_array($result, MYSQL_ASSOC)mysql_fetch_assoc($result)完全相同。

修改

如果您只想查看包含所有关键字的记录。然后你应该改变你的SQL查询,你需要在你的SQL查询中使用AND而不是OR

"SELECT * FROM `bankifscin` WHERE STATE LIKE '%$state%' AND CITY LIKE '%$city%' AND CODE LIKE '%$code%' ";

我猜不是所有关键字都在使用。所以你可能想在运行时构建那个查询。

$query = "SELECT * FROM `bankifscin` WHERE 1=1";
if ($useCity) $query .= " AND city like '%{$city_keyword}%'";
if ($useState) $query .= " AND state like '%{$state_keyword}%'";
if ($useCode) $query .= " AND code like '%{$code_keyword}%'";

我对你的问题并不了解。也许您只想包含指定的关键字? (例如$useCity = ($city_keyword != null);

修改

从我上一篇文章中的理解。您希望将输入拆分为单词。 每个必须匹配至少1个列。意味着必须使用所有单词进行查询。然后你可以试试这个:

// split the input in words
$searchq = $_POST['search'];
$words = explode(" ", $searchq);
$wordCount = count($words);

// declare which columns should be searched
$columns = array("city", "town", "code", "area");
$columnCount = count($columns);

// start from a basic query, which we will expand later
$query = "SELECT * FROM `bankifscin` WHERE ";

// all words should be used
// that's why we are adding them with the 'AND' operator.
for($i=0;$i<$wordCount;$i++)
{
  $word = mysql_escape_string($words[$i]);
  if ($i > 0) $query .= " AND ";

  // build the condition
  $condition = " (";
  for($j=0;$j<$columnCount;$j++)
  {
    // but each word can match any column, doesn't matter which one.
    // that's why we are using the 'OR' operator here. 
    if ($j > 0) $condition .= " OR ";
    $column = $columns[$j];
    $condition .= " {$column} like '%{$word}%' ";
  }
  $condition .= ") ";
  $query .= $condition;
}
echo $query;

这里的技巧是通过在运行时构建你的sql,它将产生一个完全不同的查询,具体取决于用户使用的单词数。

答案 1 :(得分:0)

我假设你只得到一排。您需要用逗号替换字符串中的空格并使用类似的东西。

$ search_sql =&#34; SELECT * FROM pfams=# SELECT version(); version ----------------------------------------------------------------------------------------------------- PostgreSQL 9.3.6 on i686-pc-linux-gnu, compiled by gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3, 32-bit WHERE STATE in($ searchq)OR CITY in($ searchq)OR CODE in($ searchq)&#34;;

答案 2 :(得分:0)

你走向错误的方向。您正在if条件下打印表格。您应该在printing循环中执行foreach部分。您只需在<table>循环之外定义foreach

您必须使用foreach循环

<table>
   <?php
       $result = mysql_query($query);
        if (!$result)
        {
          while ($row = mysql_fetch_array($result))
          {
            foreach ($row as $key => $value)
            {
               <tr>  /* (print the values ) */ </tr>
            }
          }
       }
 ?>
</table>

注意:

请停止使用mysql*,开始使用mysqli*功能

答案 3 :(得分:0)

 include('dbConnect.inc.php');

 //collect
     if(!isset($_POST['search'])){
   header("Location:index.php");

 } 
$searchq = mysql_real_escape_string( $_POST['search']);
$search_sql = "SELECT * FROM `bankifscin` WHERE STATE='%$searchq%' and`enter code here` CITY= '%$searchq%'";
$search_query = mysql_query($search_sql) or die(mysql_error());
if(mysql_num_rows($search_query)!=0){
$search_rs = mysql_fetch_assoc($search_query);
}