0

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.

1
  • 1
    Your values in the $options array are strings, so you have to quote them before inserting into your SQL string Commented Mar 27, 2013 at 0:14

1 Answer 1

2

You have to quote your options if they are strings.

$sql_unique .= "'". implode("','", $options) ."'";
Sign up to request clarification or add additional context in comments.

2 Comments

However I should do this indeed, I'm still not able to get the output I'm after. The result is now 'resource(4) of type (mysql result)' or 'Resource id #4' when echoing, no matter what the input from the array is.
Fixed it by adding: if(!$res = mysql_query($sql_unique)) { trigger_error(mysql_error().'<br />In query: '.$sql); } //elseif(mysql_num_rows($sql_unique) == 0) //{ // echo 'Geen resultaten gevonden'; //} else { $row = mysql_fetch_assoc($res); if(!$row) { echo 'The combination of topics is unique'; } else { echo $row['list'].'<br />'; } }

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.