Jump to content


Photo

Multiple ID's


  • Please log in to reply
3 replies to this topic

#1 CanMan2004

CanMan2004
  • Members
  • PipPipPip
  • Advanced Member
  • 254 posts

Posted 24 May 2006 - 07:11 PM

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

$sql = "SELECT * FROM products WHERE id = $id";

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

$sql = "SELECT * FROM products WHERE id = $id_nums";

But it doesnt seem to be able to do multiple selecting this way.

Can anyone help?

Thanks in advance

Dave

#2 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 24 May 2006 - 07:20 PM

not sure i entirely understand the question, but say you had an array of numbers

$numbers = array('45','77','79','120','134');

you can implode it into a single comma seperated string like so:

$list = implode(",", $numbers);

then you should be able to do this:

$sql = "SELECT * FROM products WHERE id IN ('$list')";

did i understand your question right?
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 CanMan2004

CanMan2004
  • Members
  • PipPipPip
  • Advanced Member
  • 254 posts

Posted 24 May 2006 - 07:54 PM

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?

#4 samshel

samshel
  • Members
  • PipPipPip
  • Advanced Member
  • 837 posts

Posted 25 May 2006 - 12:49 PM

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.....


Cheers,
SamShel
-----------------------------------------------
--: FUTURE IS LINUX :--
-----------------------------------------------




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users