Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.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 使用MySQL查询检查字符串匹配_Php_Mysql - Fatal编程技术网

Php 使用MySQL查询检查字符串匹配

Php 使用MySQL查询检查字符串匹配,php,mysql,Php,Mysql,我试图从数据表中获取所有数据行,其中一个单词与我的“故事”列中文本中的一个单词匹配 表结构: post_id | user_id | story ---------------------------------- 1 | 1 | Hello World What's up? 2 | 4 | Hello guys! 3 | 7 | Working on shareit.me! 例如: 我想抓取所有包含单词hello的帖子

我试图从数据表中获取所有数据行,其中一个单词与我的“故事”列中文本中的一个单词匹配

表结构:

post_id | user_id | story
----------------------------------
1       | 1       | Hello World What's up?
2       | 4       | Hello guys!
3       | 7       | Working on shareit.me! 
例如:

我想抓取所有包含单词hello的帖子(我正在寻找不区分大小写的)

我该怎么做

以下是我迄今为止所做的工作:

// this will be the keyword! so use the variable $filter_tag in the query!
$filter_tag= $_GET['tag'];

//query for getting the users' posts containing the select tag 
$query_posts= "SELECT * FROM posts WHERE user_id= '$user_id' ORDER BY post_id 
DESC";

$result_posts= mysqli_query($connect, $query_posts); 
谢谢

应该是:

SELECT * FROM posts WHERE ... AND story LIKE '%hello%'.
通常,“%”是一个通配符(如“*”)。因此,您可以使用“hello%”、“%hello%”等等。

应该是:

SELECT * FROM posts WHERE ... AND story LIKE '%hello%'.
 $query_posts= "SELECT * FROM posts WHERE user_id= '$user_id' AND story LIKE '%$filter_tag%' ORDER BY post_id 
    DESC";
通常,“%”是一个通配符(如“*”)。因此,您可以使用“hello%”、“%hello%”等等。

您可以使用运算符:

 $query_posts= "SELECT * FROM posts WHERE user_id= '$user_id' AND story LIKE '%$filter_tag%' ORDER BY post_id 
    DESC";
$query_posts = "SELECT * FROM posts WHERE user_id = $user_id AND story LIKE '%yourword%' ORDER BY post_id";
%
字符类似于通配符
%
表示匹配任意数量的字符,其中
(下划线)只匹配一个字符

注意:我也从您的
用户id
列检查中删除了单引号-这是一个整数,不希望在引号中-它们用于字符串。

您可以使用以下运算符:

$query_posts = "SELECT * FROM posts WHERE user_id = $user_id AND story LIKE '%yourword%' ORDER BY post_id";
%
字符类似于通配符
%
表示匹配任意数量的字符,其中
(下划线)只匹配一个字符


注意:我也从您的
用户id
列检查中删除了单引号-这是一个整数,不希望在引号中-它们用于字符串。

不用说,不要忘记转义输入

$query_posts= "SELECT * FROM posts WHERE user_id = '$user_id' AND story LIKE '%".mysql_real_escape_string($filter_tag)."%'";

不用说,别忘了转义输入

$query_posts= "SELECT * FROM posts WHERE user_id = '$user_id' AND story LIKE '%".mysql_real_escape_string($filter_tag)."%'";


我的回答是一样的,我做了一把小提琴:

#supose the tag is Hello

SELECT * FROM posts
where story like "%Hello%";

PS:

我的答案是一样的,我做了一个sqlfiddle:

#supose the tag is Hello

SELECT * FROM posts
where story like "%Hello%";

PS:

最好的解决方案是将MATCH()对()与全文索引一起使用。

最好的解决方案是将MATCH()对()与全文索引一起使用。

这是一个错误的注释。查询并不是选择包含标记的用户的所有帖子,而是选择该用户的所有帖子。是否尝试使用like运算符?一个基本的操作员应该为您这样做。记住,朋友基础是基础。我认为你应该在谷歌上搜索一下如何做到这一点。以这种方式查询是SQL最基本的功能之一。很抱歉,我不知道这些基本功能。我在学习。我想请你不要否决我的问题,因为你可以告诉我我是新来的。如果这是为了学术学习,你可以在查询中使用LIKE运算符。这是迄今为止每个人根据您的结构向您展示的内容。但如果你说的是数十万条记录,这将影响你的表现。在这种情况下,您应该查看全文搜索或使用一些额外步骤构建索引。但是,对于学习SQL的基础知识来说,这是很好的,这是一个错误的评论。查询并不是选择包含标记的用户的所有帖子,而是选择该用户的所有帖子。是否尝试使用like运算符?一个基本的操作员应该为您这样做。记住,朋友基础是基础。我认为你应该在谷歌上搜索一下如何做到这一点。以这种方式查询是SQL最基本的功能之一。很抱歉,我不知道这些基本功能。我在学习。我想请你不要否决我的问题,因为你可以告诉我我是新来的。如果这是为了学术学习,你可以在查询中使用LIKE运算符。这是迄今为止每个人根据您的结构向您展示的内容。但如果你说的是数十万条记录,这将影响你的表现。在这种情况下,您应该查看全文搜索或使用一些额外步骤构建索引。但是,对于学习SQL的基础知识来说,这是很好的。另外,请记住从“get”中转义您的值。另外,请记住从“get”中转义您的值