我希望我的搜索仅显示具有匹配字段值的结果。这是怎么做到的?
我希望我的搜索仅显示与同一行中的值匹配的结果。
我有两个搜索字段:
“搜索”和“搜索词”
我的数据库中的字段名称是
name 姓 例如
:
如果我
在“搜索”中搜索“迈克”,在“搜索词”中搜索“史密斯”,我想显示的唯一结果是与“迈克·史密斯”匹配的结果,而不是显示“迈克·哈里斯”等结果,
这是怎么回事达到了?
谢谢!
詹姆斯
<?php
$conn = mysql_connect("", "", "");
if (!$conn) {
echo "Unable to connect to DB: " . mysql_error();
exit;
}
$search = addcslashes($search,'%_');
$searchterm = addcslashes($searchterm,'%_');
$search = "%" . $_POST["search"] . "%";
$searchterm = "%" . $_POST["searchterm"] . "%";
if (!mysql_select_db("")) {
echo "Unable to select mydbname: " . mysql_error();
exit;
}
$search = $_POST['search'];
$searchterm = $_POST['searchterm'];
$sql = "SELECT name,lastname,email
FROM test_mysql
WHERE name LIKE '%". mysql_real_escape_string($search) ."%'";
$result = mysql_query($sql);
if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}
if(empty($_GET['search'])){ // or whatever your field's name is
echo 'no results';
}else{
performSearch(); // do what you're doing right now
}
if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
while ($row = mysql_fetch_assoc($result)) {
echo '<br><br><div class="data1">';
echo htmlentities($row["name"]);
echo '</div><br><div class="data2">';
echo htmlentities($row["lastname"]);
echo '</div><br><div class="data3">';
echo htmlentities($row["email"]);
echo '</div>';
}
mysql_free_result($result);
?>
I want my search to only display results that match values in the same row.
I have two search fields:
'search' and 'search term'
the field names in my database are
name
lastname
email
So for example:
If i search
'mike' in 'search' and 'smith' in 'search term' the only results I want to show are results that match 'mike smith' NOT showing results like 'mike harris'
How is this achieved?
Thanks!
James
<?php
$conn = mysql_connect("", "", "");
if (!$conn) {
echo "Unable to connect to DB: " . mysql_error();
exit;
}
$search = addcslashes($search,'%_');
$searchterm = addcslashes($searchterm,'%_');
$search = "%" . $_POST["search"] . "%";
$searchterm = "%" . $_POST["searchterm"] . "%";
if (!mysql_select_db("")) {
echo "Unable to select mydbname: " . mysql_error();
exit;
}
$search = $_POST['search'];
$searchterm = $_POST['searchterm'];
$sql = "SELECT name,lastname,email
FROM test_mysql
WHERE name LIKE '%". mysql_real_escape_string($search) ."%'";
$result = mysql_query($sql);
if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}
if(empty($_GET['search'])){ // or whatever your field's name is
echo 'no results';
}else{
performSearch(); // do what you're doing right now
}
if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
while ($row = mysql_fetch_assoc($result)) {
echo '<br><br><div class="data1">';
echo htmlentities($row["name"]);
echo '</div><br><div class="data2">';
echo htmlentities($row["lastname"]);
echo '</div><br><div class="data3">';
echo htmlentities($row["email"]);
echo '</div>';
}
mysql_free_result($result);
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
更改:
至:
Change:
To:
此外,当我查看您的查询时,您可能还需要考虑与姓氏进行匹配。您的示例表明您可能正在查找名字和姓氏,但查询只是比较名称。
我经常在可搜索表中创建一个名为“搜索”的字段,然后将它们设置为全文索引,这样我就可以使用 mysql MATCH AGAINST 来获得更强大的搜索选项。
http://dev.mysql.com/doc/refman/5.5 /en/fulltext-search.html
我的 php 开发已经有一段时间了,然后才了解 match against。尽早了解当然会有好处。希望有帮助。
Additionally, as I look over your query you may want to consider matching against last name as well. Your example suggested you may be looking for a first and last name, yet the query is only comparing name.
I often create a field called "search" in my searchable tables then set them as full text index so I can use mysql MATCH AGAINST for a more robust search option.
http://dev.mysql.com/doc/refman/5.5/en/fulltext-search.html
It was a while into my php development before I learned about match against. Certainly would have been beneficial to know early on. Hope it helps.
更改:
至:
Change:
To: