Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用explode进行PHP人员搜索_Php_Sql_Sql Server_Explode_Sqlsrv - Fatal编程技术网

使用explode进行PHP人员搜索

使用explode进行PHP人员搜索,php,sql,sql-server,explode,sqlsrv,Php,Sql,Sql Server,Explode,Sqlsrv,我想在PHP和MSSQL中使用SQLSRV驱动程序创建一个搜索函数 我在页面上只有一个搜索输入字段。 我想在我的名为“persons”的表和以下列中搜索persons:SSN、Firstname、Lastname 问题是一个人的名字列中可能有一个或多个中间名 我还希望搜索单个字符串,例如:仅搜索firstname或middelname或Lastname 我该怎么做?我应该在PHP中使用explode函数吗?我建议建立一个搜索索引 在下面的示例中,我假设您的persons表如下所示: | pe

我想在PHP和MSSQL中使用SQLSRV驱动程序创建一个搜索函数

我在页面上只有一个搜索输入字段。 我想在我的名为“persons”的表和以下列中搜索persons:SSN、Firstname、Lastname

问题是一个人的名字列中可能有一个或多个中间名

我还希望搜索单个字符串,例如:仅搜索firstname或middelname或Lastname


我该怎么做?我应该在PHP中使用explode函数吗?

我建议建立一个搜索索引

在下面的示例中,我假设您的
persons
表如下所示:

  | person_id | firstname | lastname | ssn |
为了能够构建搜索索引,您需要一个
person\u searchindex
表:

  | person_id | search_term |
然后你可以使用这样的东西:(这只是第一个草图。)


每当添加或更改person行时,添加对方法
rebuildSearchIndexForPerson()
的调用。每当删除个人行时,添加对方法
deleteSearchIndexForPerson()
的调用。第一次要搜索某个内容时,必须调用
rebuildSearchIndex()
为老年人行创建搜索索引。之后,您将能够使用
search()
函数


正如我所说的,这只是第一个草图。例如,您必须将
performQuery
escapeForQuery
getRow
方法更改为实际使用的方法。此外,php代码的质量也不是特别好。但是它的工作原理应该很清楚。

除非要修改数据模型,否则搜索速度会非常慢,因为在某个时候,您必须对整个数据集执行字符串操作。为什么?另外,如果我使用explode来更改类似的查询,您刚才说您的名字列可能包含“John Joe Jack James Joaquin”。如果你只想搜索中间名,你就必须去掉“John”。在所有的行中。显然,你可以做一个
,比如“%James%”
,然后检查该列中不是名字的行……但这也会很慢。如果有一个名为“keywords”的额外列,其中我有name、middelname和lastname,后跟一个逗号呢?嗯,我需要像:[firstname][middelname][lastname]或[firstname][lastname]或[firstname]或[lastname]或[middelname]
<?php
function addTermToSearchIndex($person_id, $term)
{
    performQuery("INSERT INTO person_searchindex (person_id, search_term)
                  VALUES (" . (int)$person_id . ", ".
                  "\"" . escapeForQuery($term) . "\");");
}

function buildSearchIndexForPerson($person_id, $firstname, $lastname, $ssn)
{
    $firstnames = explode(" ", $firstname);
    foreach($firstnames => $name)
    {
        addTermToSearchIndex($person_id, $name);
    }

    $lastnames = explode(" ", $lastname);
    foreach($lastnames => $name)
    {
        addTermToSearchIndex($person_id, $name);
    }

    addTermToSearchIndex("$ssn");
}

function deleteSearchIndexForPerson($person_id)
{
    performQuery("DELETE FROM person_searchindex WHERE person_id = " . (int)$person_id . ";");
}

function rebuildSearchIndexForPerson($person_id, $firstname, $lastname, $ssn)
{
    deleteSearchIndexForPerson($person_id);
    buildSearchIndexForPerson($person_id, $firstname, $lastname, $ssn);
}

function rebuildSearchIndex()
{
    performQuery("DELETE FROM person_searchindex;");
    $result = performQuery("SELECT * FROM persons;");
    while($row = getRow($result))
    {
         buildSearchIndexForPerson($ow["person_id"], $row["firstname"], $row["lastname"], $row["ssn"]);
    }
}

function search($searchterm)
{
     $sqlcond = "FALSE";

     $terms = explode(" ", $searchterm);
     foreach($terms => $term)
     {
         $sqlcond .= " OR s.search_term LIKE \"%" . escapeForQuery($term) . "%\"";
     }

     return performQuery("SELECT p.person_id, p.firstname, p.lastname, p.ssn FROM persons AS p LEFT JOIN person_searchindex AS s ON p.person_id = s.person_id WHERE $sqlcond;");
}

?>