2014-02-17 29 views
0

我创建了一个搜索函数。它可以通过全文查找地址,但我如何通过地址的某些部分进行查询搜索?对于例如完整的地址是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> 
+2

在sql查询中使用'LIKE %%'' – krishna

回答

1

在MySQL搜索一列是否已特定的字符串使用LIKE

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

在你的情况只是试试这个

$typed = $_POST['typed']; 

,使MySQL查询像这样

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

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

0

你忘了发表您的功能:find_patients($选择,$型); 但我猜你需要一个地址varchar字段的部分查询字符串这看起来是这样的:

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

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