下面我提到了我的搜索页面编码。它的工作正常。但我有一个小问题。例如,如果用户从组合框中选择访客姓名,然后在输入文本框中键入查询名称(例如我在我的mysql数据库中存储了Rahul Dravid)。如果他们进入" rahul dravid"在输入文本字段中,它正确显示结果。但如果他们进入" dravid"或" rahul"在输入文本字段中,它不显示结果。如何在mysql中搜索单个单词?
<form action="searchdetail.php" method="post">
SEARCH BY:
<select name="searchby">
<option value="gname">Guest Name</option>
<option value="gphone">Guest Phone</option>
</select>
<input name="query" type="text" />
<input name="subval" type="submit" value="Search" />
</form>
然后
<?php
include("config.php");
if(isset($_POST['subval']))
{
$query = mysql_real_escape_string(trim($_POST['query']));
$field = mysql_real_escape_string(trim($_POST['searchby']));
$sql = mysql_query ("SELECT * FROM voucher WHERE `$field` = '$query'");
echo "<table>";
echo "<tr>";
echo "<th width=120>Guest Name</th>";
echo "<th>Guest Phone</th>";
echo "</tr>";
while ($row = mysql_fetch_array($sql))
{
echo "<tr>";
echo "<td>" . $row['gname'] . "</td>";
echo "<td>" . $row['gphone'] . "</td>";
echo "</tr>";
}
}
?>
答案 0 :(得分:1)
您可以使用LIKE:
SELECT * FROM voucher WHERE `$field` LIKE '%$query%'
这将返回包含查询的所有结果。
示例:强>
记录:rahul dravid
rahu
drav
dravid
rahul
rahul
drav
所有人都会返回您的预期结果。
答案 1 :(得分:1)
$sql = mysql_query ("SELECT * FROM voucher WHERE `$field` LIKE '%$query%'");
答案 2 :(得分:1)
试试这个..
$sql = mysql_query ("SELECT * FROM voucher WHERE `$field` LIKE '%$query%'");
答案 3 :(得分:0)
只需编写您的查询行:
$sql = mysql_query (" SELECT * FROM voucher WHERE `$field` LIKE '%$query%' ");