473,322 Members | 1,781 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Select where clause with "IN"

41
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($mysqli, $query);

?>

[/php]
May 26 '08 #1
4 2024
rpnew
188 100+
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($mysqli, $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 Expert 1GB
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($mysqli, $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 100+
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 Expert 4TB
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
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...
1
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:...
6
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...
3
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...
1
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:...
4
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...
2
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...
11
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
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...
1
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"...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.