在子字符串中查找查询而不是全文

时间:2014-02-17 07:24:42

标签: php forms mysqli substring

我创建了一个搜索功能。它可以通过全文查找地址,但是如何通过部分地址进行查询搜索?对于例如完整地址为paya lebar Road Blk27,如何设置,以便用户只需输入paya即可显示?

SearchForm

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

3 个答案:

答案 0 :(得分:1)

使用LIKE '%$searchParam%'代替= $searchParam

答案 1 :(得分:1)

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

实施例

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

在你的情况下,试试这个

$typed = $_POST['typed'];

并像这样进行Mysql查询

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

答案 2 :(得分:0)

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

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

此类查询将返回所有在其中包含“MyPartialString”的行。