PHP搜索引擎 - 搜索多个关键字

时间:2012-10-22 08:40:25

标签: php search search-engine keyword explode

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

    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);但是返回MySQL语法错误:' ORDER BY projectid'
或者我是否必须调整我的SQL?

1 个答案:

答案 0 :(得分:1)

您只需删除explode(",", $search);部分,以便搜索字符串完好无损。你现在正在做的事情是基本上把它分开来搜索每个单词。 那应该让你的“Hans van Meulen”作为搜索字符串。