943,634 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 2090
  • PHP RSS
May 25th, 2008
0

Select where clause with "IN"

Expand 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

PHP Syntax (Toggle Plain Text)
  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]
Similar Threads
Reputation Points: 24
Solved Threads: 0
Junior Poster in Training
assgar is offline Offline
89 posts
since Oct 2006
May 26th, 2008
0

Re: Select where clause with "IN"

I'm pretty sure it has to be surrounded with quotes, ie.,
sql Syntax (Toggle Plain Text)
  1. SELECT first,last, number FROM friend WHERE id IN ('1','2','3','4')
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
May 26th, 2008
0

Re: Select where clause with "IN"

Click to Expand / Collapse  Quote originally posted by assgar ...
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

PHP Syntax (Toggle Plain Text)
  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,
php Syntax (Toggle Plain Text)
  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.
php Syntax (Toggle Plain Text)
  1. $query = "SELECT first, last, number
  2. FROM friend
  3. WHERE id IN (".$friend_implode.")";
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
May 30th, 2008
0

Re: Select where clause with "IN"

Thanks it worked.
Reputation Points: 24
Solved Threads: 0
Junior Poster in Training
assgar is offline Offline
89 posts
since Oct 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC