SitePoint Sponsor |
|
User Tag List
Results 1 to 7 of 7
-
Jun 7, 2007, 12:45 #1
- Join Date
- May 2005
- Location
- London, ON
- Posts
- 360
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
How to send an array of values to an SQL statement?
At the moment I have a database with a table called 'rankings'. In this table are unique IDs and a netValue which I use to rank people by.
I show 20 people at a time, so my current query looks like this:
PHP Code:$query = "SELECT *
FROM rankings
WHERE comp_id='".$comp_id."'
ORDER BY netWorth DESC";
($limit != "" ? $query .= " LIMIT ".$limitStart.", ".$limit : "");
AND uid = in_array($myArrayOfUIDs)
How should I go about this? I'm guessing there's a more efficient way of doing this then looping the array and adding a bunch of
PHP Code:"OR uid = '".$myArrayOfUIDs[$x]."'";
-
Jun 7, 2007, 13:02 #2
- Join Date
- Sep 2006
- Location
- Fairbanks, AK
- Posts
- 1,621
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
The quickest way I've found to do this is to use PHP's implode function to turn your array into a comma-delineated string, then use that in a MySQL IN clause, like so:
Code php:$string = implode(",", $array); $query = "SELECT * FROM table1 WHERE field1 IN ($string)";
-
Jun 7, 2007, 13:04 #3
-
Jun 7, 2007, 13:05 #4
- Join Date
- May 2002
- Location
- United States :)
- Posts
- 1,691
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Im too slow!
Silly
-
Jun 7, 2007, 13:13 #5
- Join Date
- May 2005
- Location
- London, ON
- Posts
- 360
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks guy, didn't think of that.
-
Jun 8, 2007, 12:50 #6
- Join Date
- May 2005
- Location
- London, ON
- Posts
- 360
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I've got this set up now, but its only returning the first occurrence it finds instead of all of them.
Any ideas?
-
Jun 8, 2007, 12:56 #7
- Join Date
- May 2005
- Location
- London, ON
- Posts
- 360
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Nevermind, figured it out. Even though I'm using numbers, they are being treated as strings. I needed to change the implode glue.
Thanks
Bookmarks