Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a tag system where the tags are stored inside a row as an imploded array like so: "value1, value2, value3, etc..."
And I'm trying to make a tag search page but I have no idea how to convert it to an array to see if the certain tag someone searched in is in the array(because I can't explode it whilst using SELECT).

Can anyone help or is it impossible?

Thank you all for your help, it worked.

share|improve this question
    
Don't forget to accept whatever answer has helped. –  WojtekT May 26 '12 at 21:16

5 Answers 5

There's a mysql function called FIND_IN_SET which operates on comma separated strings. For example:

SELECT * FROM posts WHERE FIND_IN_SET('some_tag', post_tags)>0

Documentation here: Mysql FIND_IN_SET

share|improve this answer

I think the better way is to normalize data and move tags to a separate table and then join tags to your entity. Search would be fast and easy =)

share|improve this answer

Read $tags_str from your data base.

Convert string to array:

   <?php
    $tags_str = "value1, value2, value3";
    $tags_arr = explode(', ', $tags_str);
    ?>

Then use in_array() PHP function for searching mathes.

share|improve this answer

You can just use "LIKE" in your select query

SELECT row FROM table WHERE tag LIKE '%search_value%'
share|improve this answer
    
This would fail if you had for example tags: 'cat food','cat food suplies'. –  WojtekT May 26 '12 at 20:58

Normalize your database with

tags {
  object_id
  tag_id
}

Or, if you're lazy, just do

UPDATE tags = CONCAT(",", tags, ",") FROM table

And then

SELECT * FROM table WHERE tags LIKE '%,selected_tag,%'
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.