Hi,
I have a database of articles. Articles are categorised by topic. I'm trying to produce a list of these articles, grouped by their respective topics.
/ FIRST LOOP, RETRIEVE TOPIC NAMES /
$sql="SELECT DISTINCT topicID, topicName FROM topics";
$result=mysql_query($sql);
while($query_data = mysql_fetch_array($result))
{
echo $query_data['topicName']."<br>";
/ SECOND LOOP, RETRIEVE ARTICLES WHICH RELATE TO THE TOPIC /
$sql2="SELECT articles.articleID, title FROM articles INNER JOIN
articles_topics ON articles.articleID = articles_topics.articleID
WHERE articles_topics.topicID=$query_data['topicID']";
$result2=mysql_query($sql2);
while($query_data2 = mysql_fetch_array($result2)) {
echo $query_data2['title'];
}
}
I've substituted numbers in the SQL statements and tested them in phpMyAdmin where they work fine. But when I try to use the code above, I get "mysql_fetch_array(): supplied argument is not a valid MySQL result resource in the line, "while($query_data2 = mysql_fetch_array($result2))".
I think the problem actually lies in $sql2 where I use "$query_data['topicID']" in the WHERE clause - but I'm not sure. Can anyone tell me what's wrong?