0

我目前正在 MySQL 数据库中搜索基于输入值的结果。提取数据后,会将其包含到数组中,然后编码为 JSON。

目前代码按预期工作,但是它不会遍历每个数据库结果,它只是提取包含输入数据的第一个结果,即使有多个匹配的结果。

例如。如果我在前端搜索表单中输入字母“e”,这应该会得到所有包含字母“e”的结果,但是它只会得到第一个带有字母“e”的结果,而不是遍历输入表(有是应该提取的其他 3 个结果)。

有没有办法修改我当前的代码以正确循环并获得与查询匹配的所有结果?

$search_string = preg_replace("/[^A-Za-z0-9]/", " ", $_POST['query']);
$search_string = $search_db->real_escape_string($search_string);

if (strlen($search_string) >= 1 && $search_string !== ' ') {
    $query = 'SELECT kbarticleid,subject FROM swkbarticles WHERE subject LIKE "%'.$search_string.'%"';
    $initial = $search_db->query($query);

    while($results = $initial->fetch_array()) {
        $result_array['kbarticleid'] = $results['kbarticleid'];
        $result_array['subject'] = $results['subject'];
    }

    $match = $result_array['kbarticleid'];

    $queryf = "SELECT * FROM swkbarticlelinks WHERE kbarticleid = '$match'";
    $final = $search_db->query($queryf);

    while($results2 = $final->fetch_array()) {
        $result_array['linktype'] = $results2['linktype'];
    }

    echo json_encode($result_array);
}

有 2 个数据库查询,因为我需要运行 2 个不同的表。

4

2 回答 2

0

请按以下方式更改您的线路

$search_string = preg_replace("/[^A-Za-z0-9]/", " ", $_POST['query']);
$search_string = $search_db->real_escape_string($search_string);

if (strlen($search_string) >= 1 && $search_string !== ' ') {
    $query = 'SELECT kbarticleid,subject FROM swkbarticles WHERE subject LIKE "%'.$search_string.'%"';
    $initial = $search_db->query($query);

    while($results = $initial->fetch_array()) {
        $result_array['kbarticleid'][] = $results['kbarticleid'];
        $result_array['subject'][] = $results['subject'];
    }

    $match_detail = $result_array['kbarticleid'];

    foreach($match_detail as $match){
        $queryf = "SELECT * FROM swkbarticlelinks WHERE kbarticleid = '$match'";
        $final = $search_db->query($queryf);

        while($results2 = $final->fetch_array()) {
            $result_array['linktype'][] = $results2['linktype'];
        }
    }

    echo json_encode($result_array);
}
于 2013-05-03T06:31:10.480 回答
-1

试试这个代码。

$search_string = preg_replace("/[^A-Za-z0-9]/", " ", $_POST['query']);
$search_string = $search_db->real_escape_string($search_string);

$result_array = array();
if (strlen($search_string) >= 1 && $search_string !== ' ') {

    $query = 'SELECT kbarticleid,subject FROM swkbarticles WHERE subject LIKE "%'.$search_string.'%"';
    $initial = $search_db->query($query);

    $i = 0;
    while($results = $initial->fetch_array()) {
        $result_array[$i]['kbarticleid'] = $results['kbarticleid'];
        $result_array[$i]['subject'] = $results['subject'];

        $queryf = "SELECT * FROM swkbarticlelinks WHERE kbarticleid = '".$results['kbarticleid']."'";
        $final = $search_db->query($queryf);


        while($results2 = $final->fetch_array()) {
            $result_array[$i]['linktype'][] = $results2['linktype'];
        }  
        $i++;
    }

    echo json_encode($result_array);
}
于 2013-05-03T06:51:46.360 回答