Jump to content

Multiple ID's


CanMan2004

Recommended Posts

Hi all

I have a field in my database called "id_nums" which store comma seperated values, such as

1,4,77,400

or

45,77,79,120,134

I 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 value

45,77,79,120,134

it would display all the rows with those id numbers

I 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 advance

Dave
Link to comment
https://forums.phpfreaks.com/topic/10382-multiple-ids/
Share on other sites

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?
Link to comment
https://forums.phpfreaks.com/topic/10382-multiple-ids/#findComment-38682
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.