Select where clause with "IN"

Thread Solved

Join Date: Oct 2006
Posts: 82
Reputation: assgar is an unknown quantity at this point 
Solved Threads: 0
assgar assgar is offline Offline
Junior Poster in Training

Select where clause with "IN"

 
0
  #1
May 25th, 2008
Hi

I would like to put a list of ids in an array
and loop to select data from a table using
"IN" in the where clause.

I am not having any success getting this to work.
Any suggestions?


Note:I am using Mysql database, apache, linux/Windows

  1. [php]
  2. <?
  3. //array with list of friends to display
  4. $friend = array('1','2','3','4');
  5.  
  6. $query = "SELECT first, last, number
  7. FROM friend
  8. WHERE id IN '$friend'";
  9. $result = mysqli_query($mysqli, $query);
[/php]
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,403
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 224
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: Select where clause with "IN"

 
0
  #2
May 26th, 2008
I'm pretty sure it has to be surrounded with quotes, ie.,
  1. SELECT first,last, number FROM friend WHERE id IN ('1','2','3','4')
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,760
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: Select where clause with "IN"

 
0
  #3
May 26th, 2008
Originally Posted by assgar View Post
Hi

I would like to put a list of ids in an array
and loop to select data from a table using
"IN" in the where clause.

I am not having any success getting this to work.
Any suggestions?


Note:I am using Mysql database, apache, linux/Windows

  1. [php]
  2. <?
  3. //array with list of friends to display
  4. $friend = array('1','2','3','4');
  5.  
  6. $query = "SELECT first, last, number
  7. FROM friend
  8. WHERE id IN '$friend'";
  9. $result = mysqli_query($mysqli, $query);
[/php]
You can't do it this way because $friend is an array. So, when you execute the query, it will be,
  1. $query = "SELECT first, last, number
  2. FROM friend
  3. WHERE id IN 'array'";
which is wrong. You can use implode function to join the array.
$friend_implode = implode(",",$friend); Then use it in your query.
  1. $query = "SELECT first, last, number
  2. FROM friend
  3. WHERE id IN (".$friend_implode.")";
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 82
Reputation: assgar is an unknown quantity at this point 
Solved Threads: 0
assgar assgar is offline Offline
Junior Poster in Training

Re: Select where clause with "IN"

 
0
  #4
May 30th, 2008
Thanks it worked.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC