Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何检查数据库字段中是否存在字符串的一部分_Php_Mysql - Fatal编程技术网

Php 如何检查数据库字段中是否存在字符串的一部分

Php 如何检查数据库字段中是否存在字符串的一部分,php,mysql,Php,Mysql,我有一个$user\u location变量,它包含当前用户所在城市,需要将该变量与数据库查询进行比较,以检查$user\u location是否与$item\u coverage类似($item\u coverage是查询的结果) $item_coverage包含由逗号“,”和$user_位置分隔的城市列表 只需遍历$item_coverage中包含的所有以逗号分隔的单词,并检查其中一个单词是否匹配$user_location,如果一个单词匹配{给出一些结果},否则如果不{给出另一个结果}您可

我有一个$user\u location变量,它包含当前用户所在城市,需要将该变量与数据库查询进行比较,以检查$user\u location是否与$item\u coverage类似($item\u coverage是查询的结果)

$item_coverage包含由逗号“,”和$user_位置分隔的城市列表

只需遍历$item_coverage中包含的所有以逗号分隔的单词,并检查其中一个单词是否匹配$user_location,如果一个单词匹配{给出一些结果},否则如果不{给出另一个结果}

您可以这样使用

$cities = explode(',', $item_coverage);

if(in_array($user_location, $cities)){
    // the city the user filled exist in the database
} else {
    // the city the user filled don't exist in the database
}

现在测试,我将与您分享结果。我的坏,您将使用LIKE和not=!可能正在尝试将
$user\u location
$cities
值设为小写或类似的值?您是否有数据库中设置的城市示例以及用户可能在城市输入中填写的内容?在
$cities=explode(“,”,$item_coverage)中缺少逗号谢谢你的回答,在我的$item_保险范围内:墨西哥城、蒙特雷、坎昆我的坏@OluwafemiSule。。我只是这么想,但是,如果所有城市都写在同一列中,并用“,”分隔,那么您可以在SQL中使用“%$city%”之类的语句!例如:
('SELECT[fields]FROM[table]WHERE[column_cities]Like“%$user_location%”)
,如果有结果,则表示该城市存在于数据库中(希望我是对的)