| Home | Register | FAQ | Members List | Search | Today's Posts | Mark Forums Read |
|
|
#1 (permalink) |
|
Senior Member
Join Date: Aug 2007
Location: California
Posts: 122
|
PHP/MySQL Query Help
I have an array containing an uncertain number of unique id's (12,4,56,32 for example). What I need to do is select rows from a MySQL table by each unique id contained within the array. How should I do this? |
|
|
|
|
|
#2 (permalink) |
|
you want it to do what!?
Join Date: Jan 2008
Posts: 22
|
you could use a WHERE ... IN sql statement Code:
HTH |
|
|
|
#3 (permalink) |
|
Everything is fine.
|
Well if you are using PHP then you can use the built in function "array_unique". Basically, take your array in IDs and parse it through that function to strip out all the non-unique ID numbers. Then all you need to do is loop through the new array and do a SELECT on each row to get the corresponding data. Barebones example: PHP Code:
Hope that helps. - Mike |
|
|
|
#5 (permalink) |
|
Everything is fine.
|
Yes I agree on that, it would be better to use one query as opposed to many. I'm guessing that the IN part of the query will ignore non-unique IDs? If not, you could still make use of the array_unique function before running the SQL query. Otherwise, forget my idea - Mike |
|
|
|
#6 (permalink) |
|
Senior Member
Join Date: Aug 2007
Location: California
Posts: 122
|
Thanks for the fast help guys. rob*, that really did the trick. Here's how I used it: PHP Code:
It's probably not the right way to do it, but it worked. |
|
|
|
#7 (permalink) |
|
shiro
Join Date: Aug 2007
Location: Yokohama, Japan
Posts: 1,812
|
That's a nice little piece of code right there. I will probably use it in the future. Although it won't work as is if you are checking strings. I think you could make it work with strings like this: Code:
Last edited by haku : 21-03-2008 at 12:36. |
|
![]() |