Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Array in Select?

0 views
Skip to first unread message

Roman Zenner

unread,
Jun 17, 2003, 5:03:40 AM6/17/03
to
Hi all,

what I am trying to do is setup a mysql query which uses an array in the
"where"-statement, like

"select * from example where id = $array[1]"

The array consist of a long list of integers which should all be looped
through. Any suggestions?

Cheers
Roman


matty

unread,
Jun 17, 2003, 11:15:18 AM6/17/03
to
Roman Zenner wrote:

You can do it with an IN () crtierion...
e.g.

$idlist = array(3,4,5,8,22,42);
$query = "select * from example where id in('";
$query .= join("', '", array_values($idlist));
$query .= "') order by somefield;";
$result = mysql_query($query, $linkid);...

$query will come out as something like

select * from example
where id in
('3', '4', '5', '8', '22', '42');

It may be, though, that you could do this with a join
somewhere in your database....
--
Matt Mitchell - AskMeNoQuestions
Dynamic Website Development and Marketing
http://www.askmenoquestions.co.uk/

Nikolai Chuvakhin

unread,
Jun 18, 2003, 4:36:22 PM6/18/03
to
"Roman Zenner" <ro...@eurode-internet.com> wrote in message
news:<bcmlh7$kde4b$1...@ID-138508.news.dfncis.de>...

$query = 'SELECT * FROM example WHERE id IN (' .
implode (',', $array) .
')';

Cheers,
NC

0 new messages