在Mysql php中连续搜索多个单词 [英] Search multiple words at a time in a row in Mysql php

查看:41
本文介绍了在Mysql php中连续搜索多个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Mysql Php 的初学者.我想制作一个搜索框,它可以找到一行,其中包含在 mysql 中搜索表的多个单词.我通过互联网获得了一个脚本,如下所示.当我输入多个单词时,它会显示搜索词最少的行.但我希望它只显示搜索多个单词的行.请帮帮我.

I am a beginner in Mysql Php. I want to make a search box which can find a row containing searching multiple words of a table in mysql. I have got a script via internet which is given below. When i type multiple words it show the row which have minimum a searching word. But I want that it will only show the row which have searching multiple words. Please Help me.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang='en'>
<head>
<META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=UTF-8'/>
<title>Page title</title>
<style type="text/css">
table {
border-collapse: collapse;
border: solid 1px black;
}
td {
padding: 2px 6px;
border: solid 1px black;
}
</style>
</head>
<body>
<form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post'>
<p>Search for: <input type='text' name='search' size='20'
maxlength='64'></p>
<p><input type='submit' value='Search'></p>
</form>
<?php
if(isset($_POST['search']))
{
$connx = mysql_connect('localhost', '*****', '*****') or
die("connx");
$db = mysql_select_db('test') or die(mysql_error());
# convert to upper case, trim it, and replace spaces with "|":
$search = (ini_get('magic_quotes_gpc')) ?
stripslashes($_POST['search']) :
$_POST['search'];
$search = mysql_real_escape_string($search);
$search = strtoupper(preg_replace('/\s+/', '|',
trim($_POST['search'])));
# create a MySQL REGEXP for the search:
$regexp = "REGEXP '[[:<:]]($search)[[:>:]]'";
$query = "SELECT * FROM `users` WHERE UPPER(`description`) $regexp
OR ".
"`name` $regexp";
$result = mysql_query($query) or die($query . " - " .
mysql_error());
echo "<table>\n";
while($row = mysql_fetch_assoc($result))
{
echo "<tr>";
foreach($row as $key => $value)
{
echo "<td>$value</td>";
}
echo "</tr>\n";
}
}
?>
</body>
</html>

推荐答案

这里有一个选择:

$search = explode(" ", $_POST['search']);
$description = "";
$name = "";
foreach($search AS $s)
{
    $description .= "`description` LIKE '%".mysql_real_escape_string($s)."%' AND ";
    $name .= "`name` LIKE '%".mysql_real_escape_string($s)."%' AND ";
}

$description = substr($description, 0, -4);
$name = substr($name, 0, -4);

$query = "SELECT * FROM `users` WHERE ($description) OR ($name)";

如果您的搜索词类似于约翰医生",您的查询将如下所示:

If your search term was something like "Doctor John" your query would look like this:

SELECT * FROM `users` WHERE (`description` LIKE '%Doctor%' AND `description` LIKE '%John%') OR (`name` LIKE '%Doctor%' AND `name` LIKE '%John%')

这显然使描述或名称字段中的两个词都成为必需.这是您要找的吗?

which obviously makes BOTH words a requirement in either the description or name field. Is this like what you were looking for?

PS - 查看 PHP 的 PDO 库.mysql_ 函数太旧了,不能再被认为是标准的了.它们会起作用,但您应该升级.

PS - look into the PDO library for PHP. mysql_ functions are too old to be considered standard anymore. They will work, but you should upgrade.

这篇关于在Mysql php中连续搜索多个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆