简体   繁体   English

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

[英]multiple keyword search using php mysql result related record

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

above is my table and i want search a multiple keyword for example 上面是我的表格,例如,我想搜索多个关键字

kerla town then it should display the record 喀拉拉邦,那么它应该显示记录

7   SBIN0000958 KERLA   CHOCHIN    TOWN

below my present code in php 下面我目前在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);
    }

to print 打印

<?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";

        }


    ?>

its only giving the result of one keyword or no result 它只给出一个关键字的结果或没有结果

You should iterate your result set, to see all records. 您应该迭代结果集,以查看所有记录。

    $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);
   }

EDIT: 编辑:

In case you wondered, the call mysql_fetch_array($result, MYSQL_ASSOC) really is the same thing as mysql_fetch_assoc($result) . 如果您想知道,调用mysql_fetch_array($result, MYSQL_ASSOC)确实与mysql_fetch_assoc($result)

EDIT: 编辑:

If you want only to see the records that contain ALL keywords. 如果只想查看包含ALL关键字的记录。 Then you should change your SQL query, you need to use AND instead of OR in your sql query. 然后,您应该更改SQL查询,您需要在SQL查询中使用AND而不是OR

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

I guess not all keywords are used at all time. 我想并不是所有的关键字都在使用。 So you may want to build that query at runtime. 因此,您可能希望在运行时构建该查询。

$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}%'";

I didn't understand that very well from your question. 我对你的问题不太了解。 Maybe you only want to include the keywords if they are specified ? 也许只指定了关键字就可以包含它们? (eg $useCity = ($city_keyword != null); ) (例如$useCity = ($city_keyword != null);

EDIT: 编辑:

From what I understood from your last post. 根据我对您上一则帖子的了解。 You want the input to be split in words. 您希望将输入分成单词。 And each word must match at least 1 column. 每个单词必须 至少匹配列。 Meaning all words must be used to query. 意味着所有单词都必须用于查询。 Then you could try this: 然后,您可以尝试以下操作:

// 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;

The trick here is that by building your sql at runtime, it will produce a totally different query depending on the number of words the user has used. 这里的窍门是,通过在运行时构建sql,它会根据用户使用的单词数产生完全不同的查询。

I am assuming that you are only getting one row. 我假设您只得到一行。 You need to replace the spaces in your string with commas and use something like this. 您需要用逗号替换字符串中的空格,并使用类似的方法。

$search_sql = "SELECT * FROM bankifscin WHERE STATE in ($searchq) OR CITY in ($searchq) OR CODE in ($searchq)"; $ search_sql =“选择*从bankifscin ($ searchq)中的状态或($ searchq)中的城市或($ searchq)中的代码”

You are going in the wrong direction. 您走错了方向。 You are printing the table in the if condition. 您将在if条件下打印表格。 You should be doing the printing part in the foreach loop. 您应该在foreach循环中进行printing You just have to define the <table> outside the foreach loop. 您只需要在foreach循环外定义<table>

You have to use foreach loop 您必须使用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>

NOTE: 注意:

Please stop using mysql* , start using mysqli* functions 请停止使用mysql* ,开始使用mysqli*函数

 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM