我正在尝试确定与论坛相关的主题组合是否是唯一的。这是在向论坛添加主题时完成的。使用此代码和查询检查唯一性:
$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);
我确信在WHERE中使用固定值时查询工作正常。使用当前代码我无法获得任何输出。无论主题组合是否唯一,vardump都会给出结果“false”。我已经好几天了,所以我真的很乐意帮助你! 提前谢谢!
答案 0 :(得分:2)
如果它们是字符串,您必须引用您的选项。
$sql_unique .= "'". implode("','", $options) ."'";