php - 查询像数组这样的列

标签 php mysql sql

在尝试将全文索引添加到我的表中后,我发现 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/

相关文章:

php - Sql 查询绑定(bind)变量与指定变量

mysql - .Net SQL查询中的动态表名?

c# - 选择数据的最佳方式是什么

javascript - 在 JavaScript 中浏览 SQL 对象

php - 使用 javascript、表单和 php 发布和提交问题

javascript - Yii2 在 Controller 中注册 JS

php - Highcharts 和 Mysql

python - 将 func.now() 从 sqlalchemy 转换为字符串

sql - 在 MS SQL Server 2005 中,有没有办法将数据库的完整维护计划导出为 SQL 脚本?

php - 我应该如何从我网站上 2 个人之间的交易中扣款?