phpmysqlarrayswhere-in

Use array valies in a WHERE IN () query


I'm trying to determine whether a combination of topics related to a forum is unique. This is done while adding topics to a forum. The uniqueness is checked with this code and query:

$options = array(); //here's your choices
$options[] = 'blablabla';
$options[] = 'blabla';

foreach($options as $key => $value) 
    {
        echo '<li>' . $value . '</li>'; 
    }

$sql_unique = "SELECT Forums_ForumID, list
           FROM (
         SELECT Forums_ForumID, GROUP_CONCAT( Topics_TopicID ) AS list
         FROM (
            SELECT *
            FROM Topics_crosstable
            ORDER BY Topics_TopicID
         )H
         GROUP BY Forums_ForumID
            )A
            WHERE list = (
         SELECT GROUP_concat( TopicID )
         FROM Topics
         WHERE Name IN (";
$sql_unique .= implode(",",$options);
$sql_unique .= ") ORDER BY Forums_ForumID ASC )";

$result = mysql_query($sql_unique);
//print "$result";
//echo $result;
//echo mysql_num_rows($result);
//$assoc = mysql_fetch_assoc($result);

var_dump($result);

I'm sure the query works fine when using fixed values in WHERE. With the current code I can't get any output. The vardump gives a result 'false' no matter if the combination of topics is unique or not.


Solution

  • You have to quote your options if they are strings.

    $sql_unique .= "'". implode("','", $options) ."'";