0

I am trying to all grab rows of data from a data table, where one word matches a word within the text in my column 'story'.

Table Structure:

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

For Example:

I want to grab all of the posts containing the word hello (I am looking for case-insensitive).

How can I do this?

Here is what I have done so far:

// 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); 

Thanks!

5
  • 1
    That is one wrong comment up there. The query is not selecting all posts of the user containing the tag, but rather all posts of the user. Commented May 3, 2013 at 13:02
  • Did you try like operator? A basic like operator should do this for you. Remember friend Basics are the basement Commented May 3, 2013 at 13:03
  • I think you should do some googling about how to do this. Querying this way is one of the most rudimentary functions of SQL. Commented May 3, 2013 at 13:04
  • I am sorry that I am unaware of the rudimentary functions. I am learning. And I would ask that you please do not down vote my question, as you can tell I am new. Commented May 3, 2013 at 13:06
  • 1
    If this is for academic learning purposes, you're fine with the LIKE operator in the query. This is what everyone has shown you so far based on your structure. But if you're talking of hundreds of thousands of records, this is going to affect your performance. In which case you should look at full-text searching or build an index using some extra steps. But again, for studying the basics of SQL, this is just fine. Commented May 3, 2013 at 13:10

7 Answers 7

3
 $query_posts= "SELECT * FROM posts WHERE user_id= '$user_id' AND story LIKE '%$filter_tag%' ORDER BY post_id 
    DESC";
Sign up to request clarification or add additional context in comments.

Comments

1
SELECT * FROM posts WHERE ... AND LOWER(story) LIKE '%hello%'

OR

SELECT * FROM posts WHERE ... 
AND story COLLATE latin1_general_ci_ai LIKE '%hello%'

Comments

0

It would be:

SELECT * FROM posts WHERE ... AND story LIKE '%hello%'.

Generally the "%" is a wildcard (like '*'). So you can use 'hello%' '%hello%' and so on.

1 Comment

Additionaly please remember to escape your values from "get"
0

You can use the LIKE operator:

$query_posts = "SELECT * FROM posts WHERE user_id = $user_id AND story LIKE '%yourword%' ORDER BY post_id";

The % characters are like wildcards. % means match any number of characters, where _ (underscore) matches just one.

Note: I've removed the single quotes from your user_id column check too - this, being an integer, doesn't want to be in quotes - they are for strings.

Comments

0

Goes without say don't forget to escape the input.

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

Comments

0

My answer is like the same, i made a sqlfiddle:

#supose the tag is Hello

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

PS: http://sqlfiddle.com/#!2/6bfcc1/5

Comments

0

The best solution will be to use MATCH() AGAINST() with an FULLTEXT index.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.