MYSQL多词搜索结果

时间:2018-07-02 11:48:02

标签: php mysql search keyword

当我输入大约三个或三个以上的关键字时,似乎只搜索第一个关键字,即螺栓螺母垫圈将仅搜索螺栓。
输入后,我想搜索多个关键字或完整的搜索词。

这是代码:

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();
}

1 个答案:

答案 0 :(得分:0)

我知道了。通过更改来解决:

. " (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 ";

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