Jump to content

Archived

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

CanMan2004

Multiple ID's

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

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites
Hi

thanks, you did understand, but when I run the statement

SELECT * FROM products WHERE id IN ('1,2,4')

it only returns row, not all the rows, why is that?

Share this post


Link to post
Share on other sites
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.....

Share this post


Link to post
Share on other sites

×

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.