Old 18-03-2008, 12:41   #1 (permalink)
mocremilo
Senior Member
 
mocremilo's Avatar
 
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?
  Reply With Quote
Old 18-03-2008, 13:18   #2 (permalink)
rob*
you want it to do what!?
 
rob*'s Avatar
 
Join Date: Jan 2008
Posts: 22
you could use a WHERE ... IN sql statement

Code:
SELECT * FROM table WHERE id IN(first_id, second_id, third_id...)
set up a foreach loop and just output the array values separated by a comma between the parenthesis.

HTH
  Reply With Quote
Old 18-03-2008, 13:23   #3 (permalink)
MikeMackay
Everything is fine.
 
MikeMackay's Avatar
 
Join Date: Feb 2005
Location: Witham & London
Posts: 744
Send a message via MSN to MikeMackay Send a message via Skype™ to MikeMackay
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:
$someIDs = array(1,2,2,4,5,6,7,2);
$uniqueIDs array_unique($someIDs);

foreach (
$uniqueIDs as $idNumber)
{
...
MySQL SELECT QUERY HERE...


Hope that helps.

- Mike
__________________
  Reply With Quote
Old 18-03-2008, 13:28   #4 (permalink)
cam
vague™
 
cam's Avatar
 
Join Date: Mar 2004
Location: Glasgow
Posts: 5,193
rob*'s example is far better. less queries.
__________________
Random goodness at The Blog

  Reply With Quote
Old 18-03-2008, 13:29   #5 (permalink)
MikeMackay
Everything is fine.
 
MikeMackay's Avatar
 
Join Date: Feb 2005
Location: Witham & London
Posts: 744
Send a message via MSN to MikeMackay Send a message via Skype™ to MikeMackay
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
__________________
  Reply With Quote
Old 18-03-2008, 13:51   #6 (permalink)
mocremilo
Senior Member
 
mocremilo's Avatar
 
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:
$ids implode(","$ids_array);
$query "SELECT * FROM table WHERE id IN(".$ids.")"

It's probably not the right way to do it, but it worked.
  Reply With Quote
Old 18-03-2008, 21:15   #7 (permalink)
haku
shiro
 
haku's Avatar
 
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:
$ids = implode("', '", $ids_array); $query = "SELECT * FROM table WHERE id IN ('".$ids."')";

Last edited by haku : 21-03-2008 at 12:36.
  Reply With Quote
Old 21-03-2008, 12:09   #8 (permalink)
prydie
Registered User
 
Join Date: Mar 2008
Posts: 7
that works, nice !
  Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Contact Us - Web Design Forums - Archive - Top
Search Engine Optimization by vBSEO 3.0.0 RC8