473,569 Members | 2,617 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Select where clause with "IN"

41 New Member
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]
<?
//array with list of friends to display
$friend = array('1','2',' 3','4');

$query = "SELECT first, last, number
FROM friend
WHERE id IN '$friend'";
$result = mysqli_query($m ysqli, $query);

?>

[/php]
May 26 '08 #1
4 2041
rpnew
188 New Member
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]
<?
//array with list of friends to display
$friend = array('1','2',' 3','4');

$query = "SELECT first, last, number
FROM friend
WHERE id IN '$friend'";
$result = mysqli_query($m ysqli, $query);

?>

[/php]
I doubt whether you can use Array in such a way... cause to use array you need to walk through it.. and i dont think PHP will do it itself.. check for that...

Regards,
RP
May 26 '08 #2
dlite922
1,584 Recognized Expert Top Contributor
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]
<?
//array with list of friends to display
$friend = array('1','2',' 3','4');

$query = "SELECT first, last, number
FROM friend
WHERE id IN '$friend'";
$result = mysqli_query($m ysqli, $query);

?>

[/php]

You need to traverse the array in a loop and build a string that goes right in the parenthesis of the IN clause.

To get you started:

[PHP]

$inStr = "";
$foo = ('a','b','c','d ')

// build string
foreach($foo as $bar)
{
$inStr = "'" . $bar . "',";
}

// remove last comma
$inStr = substr_replace( $inStr,"",-1,1);

// build SQL
$sql = "SELECT id FROM table WHERE letter IN (" . $inStr . ")";

[/PHP]

Good luck,


Dan
May 26 '08 #3
coolsti
310 Contributor
You could also use the implode function.

Say your array looks like

$foo = ('a','b','c','d ')

you can build the where part of the query like this:

Expand|Select|Wrap|Line Numbers
  1. $whereclause = "where letter in ('" . implode("','",$foo) . "')";
  2.  
May 26 '08 #4
Atli
5,058 Recognized Expert Expert
Yea, like coolsti says, the implode function will do the trick. I've used this method a few times myself.

Make sure to leave out the single-quote-marks tho if you are working with numbers. MySQL won't match numbers if they are quoted.
May 26 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
1848
by: sarith sutha | last post by:
Hi Guys Need a help i am facing a sporadic issue when executing the query using nested table of numbers Here are the things which i did 1. I created a type as CREATE TYPE NUMBERTABLE AS TABLE OF NUMBER;
1
7243
by: DB_2 | last post by:
Greetings, I was searching Google for ways to turn off transaction logging for some queries. I came across this old post from Feb 2003: > From: fareeda (fareeda@pspl.co.in) > Subject: Re: Question related to "CREATE TABLE AS SELECT" in DB2 > Newsgroups: comp.databases.ibm-db2 > Date: 2003-02-27 21:11:59 PST >
6
9322
by: GSteven | last post by:
(as formerly posted to microsoft.public.access.forms with no result) I've created a continuous form which is based on a straightforward table (ex - customers - 100 records). On the form there is a checkbox with a control source named "MARK" (boolean) from customer table. I can check and uncheck individual records fine. Then I created 2...
3
2116
by: Branco Medeiros | last post by:
Hi all, Using SQL Server 2000, SP4. I have a table of street names (Rua) whose ids (cod_rua) are foreign keys into a consumer table (Consumidor). It turns out that the "Rua" table has many unused records which I'd like to wipe out. For instance, there are some 2800 unused records in the "Rua" table, and only some 200 records actually...
1
1819
by: aiyaonline | last post by:
I like to know if the following makes any performance issue. select field_1 from table_1 where country ='USA'; (or) select field_1 from table_1 where country in ('USA'); The reason behind is: (a) For some scenario, country can only be 'USA'. (b) And for some other, country can be 'USA' or 'EUR' or 'AUS'.
4
3875
by: fran7 | last post by:
Hi, from help in the javascript forum I found the error in some code but need help. This bit of code works perfectly, trouble is I am writing it to a javascript function so the height needs to be in &quot;&quot; instead of "" otherwise I get an error message. Can anyone suggest how to write it so that it writes &quot; instead of "". I have tried all...
2
1687
by: puzzled | last post by:
I have "Find" buttons throughout my database, each with focus on a different field (they were created with the wizard, then the focus set to the corresponding field with VB). If i press a find button and change the "Look In" combo to search the form instead of a field, the next "find" button i press defaults to 'Look In' the form, not the field...
11
2003
by: bcurtu | last post by:
Hi, I have a BIIIIIG problem with the next query: cursor.execute(""" SELECT titem.object_id, titem.tag_id FROM tagging_taggeditem titem WHERE titem.object_id IN (%s) """,( eid_list))
0
701
by: Jean-Paul Calderone | last post by:
On Fri, 26 Sep 2008 14:04:35 -0500, Michael Mabin <d3vvnull@gmail.comwrote: No, not really. Particularly when it's not any harder to be secure than it is to be insecure, there's no reason to pick the insecure solution. It doesn't cost you anything to be secure. It *might* cost you something to be insecure, even if the environment is...
1
2289
by: canugi | last post by:
I need to store the contents of an SQL "in clause" in an MS Access 2000 table. I use MS Access version 9.0.8960 (SP3) This is my statement (and it works fine with the explicit "in clause" parenthesis): SELECT Permutation FROM PermutationsOne WHERE Permutation in ('B01','B02','B06'); Now, I want to store different strings similar to...
0
7701
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7615
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3643
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
940
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.