mysql多词搜索结果

3lxsmp7m  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(372)

当我键入三个或更多的关键字似乎只搜索第一个关键字,即螺栓螺母垫圈它将只搜索螺栓。
我想搜索多个关键字或完整的搜索条件时输入。
代码如下:

if (!(isset($_GET['pagenum']))) {
    $pagenum = 1;
} else {
    $pagenum = $_GET['pagenum'];
}
$page_limit = ($_GET["show"] <> "" && is_numeric($_GET["show"]) ) ? $_GET["show"] : 8;

try {
    $keyword = trim($_GET["keyword"]);
    if ($keyword <> "" ) {
        $sql = "SELECT * FROM tbl_contacts WHERE 1 AND "
. " (first_name LIKE :keyword OR contact_no1 LIKE :keyword) ORDER BY first_name ";
        $stmt = $DB->prepare($sql);
        $stmt->bindValue(":keyword", $keyword."%");
    } else {
        $sql = "SELECT * FROM tbl_contacts WHERE 1 ORDER BY first_name ";
        $stmt = $DB->prepare($sql);
    }
    $stmt->execute();
    $total_count = count($stmt->fetchAll());
    $last = ceil($total_count / $page_limit);

    if ($pagenum < 1) {
        num < 1) {
    } elseif ($pagenum > $last) {
        $pagenum = $last;
    }
    $lower_limit = ($pagenum - 1) * $page_limit;
    $lower_limit = ($lower_limit < 0) ? 0 : $lower_limit;
    $sql2 = $sql . " limit " . ($lower_limit) . " ,  " . ($page_limit) . " ";
    $stmt = $DB->prepare($sql2);

    if ($keyword <> "" ) {
        $stmt->bindValue(":keyword", $keyword."%");
        $stmt->execute();
        $results = $stmt->fetchAll();
    }
} catch (Exception $ex) {
    echo $ex->getMessage();
}
gopyfrb3

gopyfrb31#

我想出来了。通过更改修复:

. " (first_name LIKE :keyword OR contact_no1 LIKE :keyword) ORDER BY first_name ";

. " (first_name LIKE '%".$keyword."%' OR contact_no1 LIKE '%".$keyword."%') ORDER BY last_name DESC ";

现在搜索返回任何输入的关键字

相关问题