查询与数组之类的列

Bod*_*els 1 php mysql sql

在尝试任何时间将全文索引添加到我的表后,我发现像%searchword%的工作方式非常类似.我从第一个查询得到数组$keywordse,之后我使用$keywordsonetoeight = implode(', ', $keywordse[0]);逗号分隔列表创建此输出:string(25) "firstword, secondword, , , , , ,".我使用mysql,表与MyIsam一起运行.

$keywordse 看起来像这样:

    array(1) { [0]=> array(8) { ["keyword1"]=> string(0) "" ["keyword2"]=> 
string(5) "ballo" ["keyword3"]=> string(5) "ballo" ["keyword4"]=> 
string(0) "" ["keyword5"]=> string(0) "" ["keyword6"]=> 
string(0) "" ["keyword7"]=> string(0) "" ["keyword8"]=> string(0) "" } }
Run Code Online (Sandbox Code Playgroud)

我的查询:

"SELECT *
FROM posts 
WHERE title, text, area, contact LIKE %'$keywordsonetoeight'% AND (autorid != $userid)
ORDER BY id DESC"; 
Run Code Online (Sandbox Code Playgroud)

输出是 NULL

这有效:

 "SELECT *
FROM posts 
WHERE title LIKE '%firstword%' AND (autorid != $userid)
ORDER BY id DESC"; 
Run Code Online (Sandbox Code Playgroud)

Raj*_*aul 5

不要implode()直接在数组上使用函数,因为数组中确实存在多个空元素.首先取消设置数组中的空数组元素$keywordse[0],然后REGEXP用于搜索.

你的代码应该是这样的:

foreach($keywordse[0] as $key => $value){
    if(empty(trim($value))){
        unset($keywordse[0][$key]);
    }
}

$conditions = implode("|", $keywordse[0]);

$query = "SELECT *
FROM posts 
WHERE (title REGEXP '{$conditions}' 
OR text REGEXP '{$conditions}' 
OR contact REGEXP '{$conditions}') 
AND autorid <> {$userid} 
ORDER BY id DESC";

// Now execute this $query
Run Code Online (Sandbox Code Playgroud)

这是参考: