在尝试将全文索引添加到我的表中后,我发现 Like %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) "" } }
我的查询:
"SELECT *
FROM posts
WHERE title, text, area, contact LIKE %'$keywordsonetoeight'% AND (autorid != $userid)
ORDER BY id DESC";
输出为NULL
这个有效:
"SELECT *
FROM posts
WHERE title LIKE '%firstword%' AND (autorid != $userid)
ORDER BY id DESC";
最佳答案
不要直接在数组上使用 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
引用如下:
关于php - 查询像数组这样的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34703781/