View Single Post

   
  #2 (permalink)  
Old 02-28-2008, 10:32 AM
Rik Wasmus
 
Posts: n/a
Default Re: doing query for an array of strings

On Fri, 18 Jan 2008 18:52:10 +0100, yataaa <gates531@gmail.com> wrote:
> What I would like to do is this:
> SELECT * FROM 'tableName' WHERE ColumnName is in ARRAY


Depends on the database used. In MySQL, "WHERE fieldname IN
('foo','bar','foz')" is perfectly possible.

> and ARRAY has the form of a set or array, like {"foo", "bar", "raz",
> "ma", "taz"}. I can't do it like "...WHERE ColumnName='foo' OR
> ColumnName='BAR' OR ..." because I don't know those strings and how many
> of them are in the set before.


? That's no reason:

$array = array('foo','bar','baz');
$query = 'SELECT fieldname FROM tablename';
if(!empty($array)) $query .= "WHERE fieldname = '".implode("' OR fieldname
= '",$array)."'";
--
Rik Wasmus
Reply With Quote