【问题标题】:Finding query in substring instead of full text在子字符串而不是全文中查找查询
【发布时间】:2014-03-16 09:25:53
【问题描述】:

我创建了一个搜索功能。它可以通过全文查找地址,但我如何通过部分地址进行查询搜索?例如完整的地址是paya lebar Road Blk27,我怎么做才能让用户输入paya,它仍然会出现?

搜索表单

<h2>View Patient Records</h2>


<body>
<form action="display_patient.php" method="post">
<p>Select:
<select name="patient_var" >
<?php 
$value = array(view_all, name, address); 
foreach ($value as $option)
{
    echo '<option value="'.$option.'"' . (isset($_POST['patient_var']) && $_POST['patient_var'] == $option ? ' selected' : '') . '>' . $option . '</option>';
}

?>
</select>

<input type="text" name="typed" value="" />

<input type ="submit" value="submit" />
</form>
</p>

<p>

<?php
if (isset($_POST['patient_var'])) {

    $type = $_POST['typed'];
    $select = $_POST['patient_var'];
 if ($select == 'view_all') {
     echo "<table border='1'>"; 
 echo "<tr>\n"; 
echo "<th>ID</th>\n"; 
echo "<th>Patient Name</th>\n"; 
echo "<th>Age</th>\n";
echo "<th>NRIC</th>\n";
echo "<th>Birth Date</th>\n";
echo "<th>Medical Allergies</th>\n";
echo "<th>Medical History</th>\n";
echo "<th>Phone</th>\n";
echo "<th>Address</th>\n";
echo "<th>Doctor Assigned</th>\n";
 echo "</tr>"; 

    $pat_set = default_patient();

     while ($mo = mysqli_fetch_array($pat_set)) {
                echo "<tr>"; 
 echo "<td>" . $mo['id'] . "</td>"; 
 echo "<td>". $mo['name'] . "</td>"; 
  echo "<td>". $mo['age'] . "</td>"; 
   echo "<td>". $mo['nric'] . "</td>"; 
    echo "<td>". $mo['birthdate'] . "</td>";
     echo "<td>". $mo['medical_allergies'] . "</td>";
      echo "<td>". $mo['medical_history'] . "</td>";
       echo "<td>". $mo['phone'] . "</td>";     
 echo "<td>". $mo['address'] ."</td>"; 
        echo "<td>". $mo['doctor_assigned'] . "</td>";
 echo "</tr>"; 
            }
 } 




else {
    echo "<table border='1'>\n"; 
echo "<tr>\n"; 
echo "<th>ID</th>\n"; 
echo "<th>Patient Name</th>\n"; 
echo "<th>Age</th>\n";
echo "<th>NRIC</th>\n";
echo "<th>Birth Date</th>\n";
echo "<th>Medical Allergies</th>\n";
echo "<th>Medical History</th>\n";
echo "<th>Phone</th>\n";
echo "<th>Address</th>\n";
echo "<th>Doctor Assigned</th>\n";
 echo "</tr>";

  $patients_set = 
 find_patients($select, $type);

 while ($row = mysqli_fetch_array($patients_set)) 
 { echo "<tr>"; 
 echo "<td>" . $row['id'] . "</td>"; 
 echo "<td>". $row['name'] . "</td>"; 
  echo "<td>". $row['age'] . "</td>"; 
   echo "<td>". $row['nric'] . "</td>"; 
    echo "<td>". $row['birthdate'] . "</td>";
     echo "<td>". $row['medical_allergies'] . "</td>";
      echo "<td>". $row['medical_history'] . "</td>";
       echo "<td>". $row['phone'] . "</td>";     
 echo "<td>". $row['address'] ."</td>"; 
        echo "<td>". $row['doctor_assigned'] . "</td>";
 echo "</tr>"; }

}  

}//end of if post submit

?>
</p>

【问题讨论】:

  • 在sql查询中使用LIKE %%

标签: php forms mysqli substring


【解决方案1】:

在Mysql中用于搜索列是否有特定字符串使用LIKE

例子

SELECT * FROM table WHERE column LIKE '%somestring%'; 

在你的情况下试试这个

$typed = $_POST['typed'];

并像这样进行 Mysql 查询

$query = "select * from table where Address LIKE '%".$typed."%' ";

【讨论】:

    【解决方案2】:

    使用LIKE '%$searchParam%' 而不是= $searchParam

    【讨论】:

      【解决方案3】:

      您忘记发布您的函数:find_patients($select, $type); 但我猜你需要一个地址 varchar 字段的部分字符串查询,看起来像这样:

      select * from MyTable where myColumn like '%myPartialString%';
      

      这种类型的查询将返回所有包含“MyPartialString”的行。

      【讨论】:

        猜你喜欢
        • 2021-02-15
        • 1970-01-01
        • 2013-07-05
        • 1970-01-01
        • 2018-12-02
        • 2012-08-18
        • 1970-01-01
        • 2017-07-16
        • 1970-01-01
        相关资源
        最近更新 更多