![]() |
MySQL Check List and Check String & Forms
Hey all! I am trying to figure out if there is an easy way to check if a value is in a (MySQL) table column which saves values as a comma separated list. So for example, if the column name is "favorites", and for some user favorites = "54,23,12", in a query can I select only the rows that have 23 in their "favorites" column?
Another issue I need assistance with is trying to find if a number is within a list of numbers separated by commas. So let's say I have a list of numbers "52,13,2,5". I want to see if 2 is in this list. I cant convert to strings because if I used a string operator and search for 2 it would show as true or give me the the place where it found the 2 in 52. Any help would be great! |
Assuming you already know how to get that value from the database and you habe it in a variable for example $favorites you can use this code
PHP Code:
PHP Code:
|
If in your first example I replaced 23 with 2, wouldn't it give me the the place where it shows 2 in 52 before it check if there is 2 alone?
Also, in my first question I wanted to do this in MySQL without having to run another query on data I already retrieved. |
You can use LIKE in a WHERE statement like:
PHP Code:
|
Then you need the second one, i couldn't think of an easier way to do it right now
|
Quote:
Quote:
|
Quote:
And second question: yes. |
Thanks for your help guys. I'm learning more and more every day! I'm already designing my own plugins and tools. If I figure out how to export them and do the whole process to make them their own thing I might be able to release one! :)
--------------- Added [DATE]1406422797[/DATE] at [TIME]1406422797[/TIME] --------------- figured I'd keep this one to ask again. I am creating a form in the ACP to update a table one of my forum systems uses. I successfully get the correct info when I click the edit button, but the form that shows up is not selecting the proper options for my selection items even though I have passed the info one. Code below. PHP Code:
|
There's a mysql function called FIND_IN_SET that might work. You'd have something like
Code:
WHERE FIND_IN_SET('23', favorites) != NULL |
Just an observation...don't kill me for it...but storing comma values always kinda sends the alarms off. It's breaking First Normal Form which, if I were just learning, I'd want to do as little as possible.
Are you sure there isn't a better way to store your data? |
All times are GMT. The time now is 03:38 AM. |
Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information | |
---|---|
|
|
![]() |
|
Template Usage:
Phrase Groups Available:
|
Included Files:
Hooks Called:
|