vForums Support > Programming & Coding :: Programming Discussion :: > Fantastic MySQL Trick
Fantastic MySQL Trick - Posted By Marc (cr0w) on 10th Jun 10 at 2:09am
A few months ago, I came across this little MySQL function while working on a site, and I assumed that everyone knew about it and I'd just been ignorant, but since my discovery I've yet to find someone else who knew about it. So, I thought I'd share!
Say you have a messaging system that allows for multiple users, and stores the message participants in a CSV in the column `users` (Ex: 1,4,16,18). How would you usually see if the current user is in that list?
You'd grab the `users` list, explode() it, and loop through the array to see if it matched the cookie...well not anymore!
SELECT * FROM messages WHERE FIND_IN_SET($_COOKIE['user'], users)
For those of you who didn't know about this gem, enjoy!
For those of you who did know, shame on you for not sharing.
Re: Fantastic MySQL Trick - Posted By dog199200 (dog199200) on 10th Jun 10 at 3:29am
interesting, well this might actually helps solve a problem I've been having.
Re: Fantastic MySQL Trick - Posted By Michael (wrighty) on 10th Jun 10 at 7:50am
Seems handy!
I might have a use for it in my current job!
Re: Fantastic MySQL Trick - Posted By dog199200 (dog199200) on 10th Jun 10 at 3:38pm
speaking of jobs, marc, or michael, either of you wanting to make a little money XD Na I think I can manage, just getting bored of coding XD
Re: Fantastic MySQL Trick - Posted By Michael (wrighty) on 10th Jun 10 at 5:19pm
I code 9-5 every day. I have a freelance client lined up, a project for someone, and improvements to do to wImg... I'm fully booked!
Re: Fantastic MySQL Trick - Posted By Ross (admin) on 10th Jun 10 at 5:20pm
I probably wouldn't explode, I'd use some kind of RegEx
SELECT * FROM messages WHERE `users` REGEXP '(^|,)ID_HERE(,|$)'
Handy to know though. I have used a MySQL function (which I forget the name of now) to do the opposite - check if the value in the database is one of a set list (in fact I think vForums uses it in a couple of places)
Re: Fantastic MySQL Trick - Posted By dog199200 (dog199200) on 10th Jun 10 at 6:25pm
XD yea well i tend not to use a lot of fancy coding, which is why i dont mind highering someone to look over my coding and do some editing to make it more secure and faster XD as for calling information, i think the most extreme i've done is:
"SELECT * FROM random WHERE random_type='random'" or using escape() within an sql query XD