user1701398
user1701398

Reputation: 1669

Grabing data from a column that has more then one value, with php?

Basically I have products data stored in a database which also have a field called "tags" which is designed to store tags about that specific product. These tags are stored in a format like so:

"tag1,tag2,tag3,tag4"

The question is say that I want to have a page where a user can view all the latest products for that specific tag. How can I possibly accomplish this without taking every single product, fetching the "tag" field, separating them and then finally checking if that specific tag exists in there?

Is it possible to do this with a mysql command?

I am sorry I didnt post any code, I just am not sure how I should go about doing something like this in general.

Thank you!

Upvotes: 0

Views: 32

Answers (2)

moonwave99
moonwave99

Reputation: 22817

You should normalize your database, so you can perform the search straight via SQL query.

Upvotes: 1

Explosion Pills
Explosion Pills

Reputation: 191749

Probably not the most efficient way, but:

WHERE tags LIKE '%$search_tag%'

This assumes that a tag that is searched for cannot be contained by another tag, or you'd need:

WHERE
   tags LIKE '%,$search_tag%'
   OR tags LIKE '%$search_tag,%'
   OR tags = '$search_tag'

Obviously, search_tag needs to be escaped properly.

Upvotes: 1

Related Questions