如果只输入一个字母,PHP 搜索结果会输出不同的结果

标签 php mysql search

如果我输入 2 个或更多字母,我正在使用的搜索代码适用于搜索,但是当我用一个字母给它一个查询时,它会为我提供所有数据列表,我有一个像“找到”子句这样的位置状态但如果我输入一个字母,它仍然会给出其他结果。如果我正确输入两个或多个字母,搜索部分就会起作用,但如果我输入一个字母,它会给出不同的查询结果。

这是我的 SQL 查询

> $searchtext = ''; if(isset($_GET['q'])) $searchtext =
> mysql_real_escape_string($_GET['q']); if($searchtext) {
>      $per_page =16;
>                         $pages_query = mysql_query("SELECT COUNT('PersonID') FROM persons where firstname like '$searchtext' or
> lastname like '$searchtext' and status like 'found' ");
>                         $pages = ceil(mysql_result($pages_query,0) / $per_page);
> 
>                         $page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
>                         $start = ($page - 1) * $per_page;
> 
> 
> // And set the first page $first_page = "1";
> 
>      $num = mysql_num_rows($pages_query);
>     $last_page = ($num / $per_page);
>     $next_page = $page + 1;
>     $last_page=$pages;
> 
> 
> 

>     $query=mysql_query("select * from persons where status like 'found' and firstname like '%$searchtext%' or lastname like
> '%$searchtext%' order by date desc LIMIT $start,$per_page ");
> 
>    $numrows = mysql_num_rows($query);
> 
> 
> }
> 
> else  {
> 
>    $per_page =16;
>                         $pages_query = mysql_query("SELECT COUNT('PersonID') FROM persons where status like 'found' ");
>                         $pages = ceil(mysql_result($pages_query,0) / $per_page);
> 
>                         $page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
>                         $start = ($page - 1) * $per_page;
>                         $last_page=$pages;
> 
> // And set the first page $first_page = "1";
> 
> 
>     $query=mysql_query("select * from persons where status like 'found'  order by date desc LIMIT $start,$per_page ");   
> $count=mysql_query("select * from persons where status like 'found'");
> $numrows = mysql_num_rows($count);
> 
> }

此屏幕截图是默认 View 。 其查询是 $query=mysql_query("select * from people where status like 'found' order by date desc LIMIT $start,$per_page "); 1st screen shot

此屏幕截图是当我在默认 View 中搜索“dan”以查找并收到此结果时的搜索 View ,这是我用于输出的查询。 ` $query=mysql_query("从状态为 'found' 且名字为 '%$searchtext%' 或姓氏为 '%$searchtext%' 的人员中选择 * order by date desc LIMIT $start,$per_page ");

$numrows = mysql_num_rows($query);` 2nd picture

最后一张图片显示我搜索了字母a。给我的结果来自各个领域。另外,我有一个冲突,它也称为丢失列表,我想将其限制为仅针对找到的列表,这也是它在找到的整个字段中找到的丢失查询的问题。

我这里使用的SQL命令是图2中的命令 picture3

最佳答案

您的 SQL 查询需要对条件进行正确分组

$query=mysql_query("select * from persons where status like 'found' and (firstname like '%$searchtext%' or lastname like '%$searchtext%') order by date desc LIMIT $start,$per_page ");

您所做的相当于代码中的类似操作:

if (status == 'found' && firstname like 'a')
{
    return row;
}
else if (lastname like 'a')
{
    return row;
}

如果你看到这个,可能会更清楚一点,如果第一个条件失败(唯一一个检查 status == 'found' 的条件,那么它将跳到下一个条件(在OR 语句)并仅检查 lastname 是否匹配。

您需要对 select 语句条件进行分组,以便它正确检查:

...where status like 'found' and (firstname like '%$searchtext%' or lastname like '%$searchtext%') order by...

在本例中,我们将 firstnamelastname 条件分组在一起(在括号中),以便整个 OR 条件得出单个真/假答案,即答案然后应用于之后的状态检查。

关于如果只输入一个字母,PHP 搜索结果会输出不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18239282/

相关文章:

php - json_encode 使用 Postgres 数据库和 php 返回 null

php - 无法使用 PHP 5.6.3 与 Composer 建立 SSL 连接

mysql - MySQL 中传递给 IN() 的最大项目数是多少

当恰好有 404 个匹配项时,Eclipse 搜索结果丢失?

php - 在 SQL 中是否可以将连字符和空格视为相同?

javascript - 如何使用 PHP 在 DropZone 中触发错误事件?

php - 在 PHP 中获取 SEO 友好 URL 的问题

php - Laravel 5.1 无法迁移数据库

mysql - 在 laravel 查询中增加 +1 的更好方法?

algorithm - 是否也可以使用搜索算法(BFS 和 DFS)来获得最短路径?