Php 使用类似数组的列进行查询
在尝试在我的表中添加全文索引几个小时后,我发现Like%searchword%的工作原理也是如此。我从第一个查询中得到数组Php 使用类似数组的列进行查询,php,mysql,sql,Php,Mysql,Sql,在尝试在我的表中添加全文索引几个小时后,我发现Like%searchword%的工作原理也是如此。我从第一个查询中得到数组$keywordse,然后用$keywordsonetoeight=introde(',',$keywordse[0])创建数组一个以逗号分隔的列表,输出:字符串(25)“第一个字,第二个字,,,,”。我使用mysql,表使用MyIsam运行 $keywordse如下所示: array(1) { [0]=> array(8) { ["keyword1"]=>
$keywordse
,然后用$keywordsonetoeight=introde(',',$keywordse[0])创建数组代码>一个以逗号分隔的列表,输出:字符串(25)“第一个字,第二个字,,,,”
。我使用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) "" } }
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
我的查询:
"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";
如果要比较数组中的所有值,可以使用REGEXP:
"SELECT *
FROM posts
WHERE title REGEXP '$first_word|$second_word|$third_word'
OR text REGEXP '$first_word|$second_word|$third_word' ) AND (autorid != $userid)
ORDER BY id DESC";
如果要比较数组中的所有值,可以使用REGEXP:
"SELECT *
FROM posts
WHERE title REGEXP '$first_word|$second_word|$third_word'
OR text REGEXP '$first_word|$second_word|$third_word' ) AND (autorid != $userid)
ORDER BY id DESC";
不要直接在数组中使用内爆()
函数,因为数组中确实存在多个空元素。首先从$keywordse[0]
数组中取消设置空数组元素,然后使用REGEXP
进行搜索
您的代码应该如下所示:
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) "" } }
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
foreach($keywordse[0]作为$key=>$value){
if(空(修剪($value))){
未设置($keywordse[0][$key]);
}
}
$conditions=内爆(“|”,$keywordse[0]);
$query=“选择*
发帖
其中(title REGEXP'{$conditions}'
或文本REGEXP“{$conditions}”
或联系REGEXP“{$conditions}”)
和自动标识{$userid}
按id描述订购”;
//现在执行这个$query
以下是参考资料:
不要直接在数组中使用内爆()
函数,因为数组中确实存在多个空元素。首先从$keywordse[0]
数组中取消设置空数组元素,然后使用REGEXP
进行搜索
您的代码应该如下所示:
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) "" } }
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
foreach($keywordse[0]作为$key=>$value){
if(空(修剪($value))){
未设置($keywordse[0][$key]);
}
}
$conditions=内爆(“|”,$keywordse[0]);
$query=“选择*
发帖
其中(title REGEXP'{$conditions}'
或文本REGEXP“{$conditions}”
或联系REGEXP“{$conditions}”)
和自动标识{$userid}
按id描述订购”;
//现在执行这个$query
以下是参考资料:
这并没有改变什么,我得到了“NULL”。谢谢,我需要第二个查询,但我得到了NULL,如果我用posts中某个列中存在的单词来更改一个“%$keywordsonetoeight%”,它将与您的代码一起工作。数组中的查询语法一定有问题。更新后的查询没有什么变化,我得到了“NULL”。谢谢,我需要第二个查询,但我得到了NULL,如果我用posts中某列中存在的单词更改一个“%$keywordsonetoeight%”,它将与您的代码一起工作。数组中的查询语法一定有问题。在更新之后,在我得到`数组(0){}之前,也要对查询进行排序。但是我检查了数组值是否与column@Bodoppels您是否仍然得到NULL
?如果是,请用$keywordse
array的内容更新您的问题。否我得到array(0){}
。如果我回显$conditions
我得到字符串(17)“| ballo | ballo | | | |”
。这些列有索引并且是全文的。可能有问题吗?@Bodoppels发现了这个错误<代码>$conditions=内爆(“|”,$keywordse)
应该是$conditions=infrade(|“,$keywordse[0])代码>。我已经更新了我的答案。@Bodoppels很高兴我能帮上忙。干杯!:)这里我也得到了`数组(0){}'。但是我检查了数组值是否与column@Bodoppels您是否仍然得到NULL
?如果是,请用$keywordse
array的内容更新您的问题。否我得到array(0){}
。如果我回显$conditions
我得到字符串(17)“| ballo | ballo | | | |”
。这些列有索引并且是全文的。可能有问题吗?@Bodoppels发现了这个错误<代码>$conditions=内爆(“|”,$keywordse)
应该是$conditions=infrade(|“,$keywordse[0])代码>。我已经更新了我的答案。@Bodoppels很高兴我能帮上忙。干杯!:)