我想创建一个搜索框,而不是在查询中使用 WHERE cluse 来匹配字段中的确切短语,我希望搜索框能够执行语义搜索,以便当用户输入部分短语时,它会在能够匹配短语含义的情况下显示合适的结果。当您尝试通过 Google 进行搜索时,Google 究竟是如何工作的。这有可能实现吗?有人有什么想法吗?
下面是我目前使用的代码,以便能够使用数据库来查看一行是否与在搜索框中输入的确切短语匹配:
<?php
//connected to DB
foreach (array('questioncontent') as $varname) {
$questioncontent = (isset($_POST[$varname])) ? $_POST[$varname] : '';
}
?>
<p>Search for a previous question by entering in a phrase in the search box below and submitting the phrase</p>
<form action="previousquestions.php" method="post" id="modalform">
<p>Search: <input type="text" name="questioncontent" value="<?php echo $questioncontent; ?>" /></p>
<p><input id="searchquestion" name="searchQuestion" type="submit" value="Search" /></p>
</form>
<?php
if (isset($_POST['searchQuestion'])) {
$questionquery = "SELECT QuestionContent FROM Question
WHERE(QuestionContent = '".mysql_real_escape_string($questioncontent)."')";
$questionnum = mysql_num_rows($questionresult = mysql_query($questionquery));
if($questionnum !=0){
$output = "";
while ($questionrow = mysql_fetch_array($questionresult)) {
$output .= "
<table>
<tr>
<td>{$questionrow['QuestionContent']}</td>
</tr>";
}
$output .= " </table>";
echo $output;
}
}