1

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.

1
  • Don't forget to accept whatever answer has helped.
    – WojtekT
    Commented May 26, 2012 at 21:16

5 Answers 5

3

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

1

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 =)

0

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.

0

You can just use "LIKE" in your select query

SELECT row FROM table WHERE tag LIKE '%search_value%'
1
  • This would fail if you had for example tags: 'cat food','cat food suplies'.
    – WojtekT
    Commented May 26, 2012 at 20:58
0

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,%'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.