montgomery Posted August 2, 2007 Share Posted August 2, 2007 Hi guys, I am a complete newbie in PHP / MySQL.... This is my problem: I have a field in the DB with numbers (=$PRESELECTED) The numbers are seperated with commas. Sometimes it is just one number. Sometimes it can be 30 numbers. If it would always be 5 numbers I could use them like this. $PreselectedSingle= explode(',',$PRESELECTED); $qs = "SELECT * FROM XL_auctions WHERE id= $PreselectedSingle[0] OR id= $PreselectedSingle[1] OR id= $PreselectedSingle[2] OR id= $PreselectedSingle[3] OR id= $PreselectedSingle[4];" But I can not predict how many numbers are in the field. I tried the PHP WHILE or FOR functions. But it does not work in this SQL statement. What can I do? Thanks so much for your help! Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted August 2, 2007 Share Posted August 2, 2007 If you have a comma separated list, you can use the IN clause: $qs = "SELECT * FROM XL-auctions WHERE id IN ($PRESELECTED)"; Quote Link to comment Share on other sites More sharing options...
clearstatcache Posted August 2, 2007 Share Posted August 2, 2007 how about using a foreach.. $PreselectedSingle= explode(',',$PRESELECTED); foreach($PreselectedSingle as $value) { $qs = "SELECT * FROM XL_auctions WHERE id= $value"; } Quote Link to comment Share on other sites More sharing options...
montgomery Posted August 2, 2007 Author Share Posted August 2, 2007 If you have a comma separated list, you can use the IN clause: $qs = "SELECT * FROM XL-auctions WHERE id IN ($PRESELECTED)"; Thanks guys. You are great! It worked. Quote Link to comment Share on other sites More sharing options...
fenway Posted August 2, 2007 Share Posted August 2, 2007 how about using a foreach.. $PreselectedSingle= explode(',',$PRESELECTED); foreach($PreselectedSingle as $value) { $qs = "SELECT * FROM XL_auctions WHERE id= $value"; } What a terrible idea... why do ppl like to hammer the DB and use PHP for everything? Quote Link to comment Share on other sites More sharing options...
clearstatcache Posted August 3, 2007 Share Posted August 3, 2007 sorry then...haven't used that function in mysql.....anywz...tnx for the new learning Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.