1

我的搜索引擎有这个代码:

    if(isset($_POST['search']))
    {
        $search = $_POST['search'];
        $terms = explode(",", $search);
        $projectlistquery = "
            SELECT project.accid, customer.custid, projectid, project.datefrom, custnamecode, dateto, daluur, project.gebruikersid, gebruikers.gebruikersid, 
            type, typeid, typename, custsurname, custmidname, custforename, acc1cust, acc2date, acc3nrofproj, acc4user, project.crew, project.jobinstr, project.projectinfo, usersurname, usermidname, userforename
            FROM project
                INNER JOIN type on project.type = type.typeid
                INNER JOIN customer on project.custid = customer.custid
                INNER JOIN accountingcode on project.accid = accountingcode.accid
                INNER JOIN gebruikers on project.gebruikersid = gebruikers.gebruikersid         
            WHERE
        ";
        $i = 0;
        foreach ($terms as $each) 
        {
            if ($i++ > 0) 
            $projectlistquery .= ' OR ';

            $projectlistquery .= "concat(custsurname, custforename, custmidname, custpostal, custphone1, custphone2, custfax, custnamecode, acc1cust, acc2date, acc3nrofproj, acc4user, typename, usersurname, usermidname, userforename) LIKE '%$each%' ";
        }
        $projectlistquery .= " ORDER BY projectid ";
    }
    else
    $projectlistquery= "    // query to show all records.

现在,这对于搜索 1 个密钥完全有效。

如果我搜索一个名字:Hans van Meulen,它会显示所有带有 Hans 的名字,所有带有“van”的名字和 Meulen。因此,如果我的数据库中有 Dude van Berkel,他也会出现。我想一起搜索 3 个字符串。

这可以通过 IMPLODE 实现吗?$terms = implode(",", $search); 但是,在“ORDER BY projectid”处返回 MySQL 语法错误。
还是我必须调整我的 SQL?

4

1 回答 1

1

您只需删除该explode(",", $search);部分,以便您的搜索字符串完好无损。您现在所做的基本上是将其撕裂以搜索每个单词。这应该让您将“Hans van Meulen”作为搜索字符串。

于 2012-10-22T08:45:01.840 回答