CanMan2004 Posted May 24, 2006 Share Posted May 24, 2006 Hi allI have a field in my database called "id_nums" which store comma seperated values, such as1,4,77,400or45,77,79,120,134I then have a php query which looks like[code]$sql = "SELECT * FROM products WHERE id = $id";[/code]At the moment is basically is selecting all rows which match the id number, what I want to do is display all row id numbers which match the comma seperated value "id_nums".So if it passed the value45,77,79,120,134it would display all the rows with those id numbersI made[code]$sql = "SELECT * FROM products WHERE id = $id_nums";[/code]But it doesnt seem to be able to do multiple selecting this way.Can anyone help?Thanks in advanceDave Quote Link to comment https://forums.phpfreaks.com/topic/10382-multiple-ids/ Share on other sites More sharing options...
.josh Posted May 24, 2006 Share Posted May 24, 2006 not sure i entirely understand the question, but say you had an array of numbers[b]$numbers = array('45','77','79','120','134');[/b]you can implode it into a single comma seperated string like so:[b]$list = implode(",", $numbers);[/b]then you should be able to do this:[b]$sql = "SELECT * FROM products WHERE id IN ('$list')";[/b]did i understand your question right? Quote Link to comment https://forums.phpfreaks.com/topic/10382-multiple-ids/#findComment-38682 Share on other sites More sharing options...
CanMan2004 Posted May 24, 2006 Author Share Posted May 24, 2006 Hithanks, you did understand, but when I run the statementSELECT * FROM products WHERE id IN ('1,2,4')it only returns row, not all the rows, why is that? Quote Link to comment https://forums.phpfreaks.com/topic/10382-multiple-ids/#findComment-38690 Share on other sites More sharing options...
samshel Posted May 25, 2006 Share Posted May 25, 2006 use it like this...SELECT * FROM products WHERE id IN ('1','2','4');when u enclose all numbers in a single quote it acts as one element..... Quote Link to comment https://forums.phpfreaks.com/topic/10382-multiple-ids/#findComment-38891 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.