0

I have a string:

 $string = "12,15,22";

Each of these values represents a usrID and I'd like to use these to select the rows associated with the $string.

Something like this:

SELECT usrFirst, usrLast FROM tblusers WHERE usrID = 12 OR 15 OR 22.

Now, the $string changes all the time, and sometimes can be a different number of ID's. Eg., sometimes 3, sometimes 4.

Thanks!

Dodinas
  • 6,705
  • 22
  • 76
  • 108

3 Answers3

3

use IN, although it is very slow if you have lots of numbers:

SELECT usrFirst, usrLast FROM tblusers WHERE usrID IN ($string)

W3Schools has more info on IN.

If you have lots of numbers, have a look here for info on how to speed this up.

Community
  • 1
  • 1
Marius
  • 57,995
  • 32
  • 132
  • 151
1

you can do something like:

SELECT usrFirst, usrLast FROM tblusers WHERE usrID in (12, 15, 22);

John Boker
  • 82,559
  • 17
  • 97
  • 130
1

you can also do:

$string = str_replace(',',' OR usrID=',$string);
mysql_query('SELECT usrFirst, usrLast FROM tblusers WHERE usrID='.$string);
Alex S
  • 25,241
  • 18
  • 52
  • 63