我希望我的搜索仅显示具有匹配字段值的结果。这是怎么做到的?

发布于 2024-12-12 02:27:21 字数 1595 浏览 0 评论 0原文

我希望我的搜索仅显示与同一行中的值匹配的结果。

我有两个搜索字段:

“搜索”和“搜索词”

我的数据库中的字段名称是

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

缪败 2024-12-19 02:27:21

更改:

WHERE  name LIKE '%". mysql_real_escape_string($search) ."%'";

至:

WHERE  name LIKE '%". mysql_real_escape_string($search) ."%' AND lastname LIKE '%". mysql_real_escape_string($search) ."%'";

Change:

WHERE  name LIKE '%". mysql_real_escape_string($search) ."%'";

To:

WHERE  name LIKE '%". mysql_real_escape_string($search) ."%' AND lastname LIKE '%". mysql_real_escape_string($search) ."%'";
瀟灑尐姊 2024-12-19 02:27:21
$sql = "SELECT name,lastname,email
FROM   test_mysql
WHERE  name LIKE '%". mysql_real_escape_string($search) ."%'
AND name LIKE '%". mysql_real_escape_string($searchterm) ."%'";

此外,当我查看您的查询时,您可能还需要考虑与姓氏进行匹配。您的示例表明您可能正在查找名字和姓氏,但查询只是比较名称。

我经常在可搜索表中创建一个名为“搜索”的字段,然后将它们设置为全文索引,这样我就可以使用 mysql MATCH AGAINST 来获得更强大的搜索选项。

http://dev.mysql.com/doc/refman/5.5 /en/fulltext-search.html

我的 php 开发已经有一段时间了,然后才了解 match against。尽早了解当然会有好处。希望有帮助。

$sql = "SELECT name,lastname,email
FROM   test_mysql
WHERE  name LIKE '%". mysql_real_escape_string($search) ."%'
AND name LIKE '%". mysql_real_escape_string($searchterm) ."%'";

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.

夜巴黎 2024-12-19 02:27:21

更改:

$search = addcslashes($search,'%_');
$searchterm = addcslashes($searchterm,'%_');

$search = "%" . $_POST["search"] . "%";
$searchterm = "%" . $_POST["searchterm"] . "%";

至:

$search = "%" . addcslashes($_POST["search"],'%_'). "%";
$searchterm = "%" . addcslashes($_POST["searchterm"],'%_'). "%";

Change:

$search = addcslashes($search,'%_');
$searchterm = addcslashes($searchterm,'%_');

$search = "%" . $_POST["search"] . "%";
$searchterm = "%" . $_POST["searchterm"] . "%";

To:

$search = "%" . addcslashes($_POST["search"],'%_'). "%";
$searchterm = "%" . addcslashes($_POST["searchterm"],'%_'). "%";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文