Jump to content

3 tables, 1 query


jabbamonkey

Recommended Posts

I have three tables I\'m trying to combine. Two tables have information regarding a subject, and the third table is what I call the \"Connection\" table. I have the following query....


SELECT 

 a.comment_id, 

 a.name, 

 a.email, 

 a.comment, 

 a.live, 

 b.article_name,

 b.article_id,

 c.article_id, 

 c.comment_id, 

 c.commentjuke_id 

FROM 

 comments as a, 

 articles as b, 

 connect_commentarticles as c 

WHERE 

 c.article_id=\'$id\' 

 AND 

 a.comment_id=c.comment_id 

 AND 

 a.live=\'1\'";

Basically, Comments for articles are stored in one table. Articles content is stored in the other, and the third table pulls the two tables together (only three columns, for the article id, the comments id, and it\'s own id). I just want to pull the tables together but keep getting an error.

 

NOTE: the variable \"$id\" is the article id, that will be submitted by the user.

 

Please let me know if you can help!

Link to comment
https://forums.phpfreaks.com/topic/812-3-tables-1-query/
Share on other sites

Would this be right?

 

SELECT 

 a.comment_id, 

 a.name, 

 a.email, 

 a.comment, 

 a.live, 

 b.article_name, 

 b.article_id, 

 c.article_id, 

 c.comment_id, 

 c.commentjuke_id 

FROM 

 comments as a, 

 articles as b, 

 connect_commentarticles as c 

WHERE 

 b.article_id=\'$id\' 

 AND 

 c.article_id=b.article_id

 AND 

 a.comment_id=c.comment_id 

 AND 

 a.live=\'1\'";

Link to comment
https://forums.phpfreaks.com/topic/812-3-tables-1-query/#findComment-2690
Share on other sites

I keep getting an error ...

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in...

 

Does it matter the order of the WHERE clause? For example, does it matter if \"b.article_id=c.article_id\" or \"c.article_id=b.article_id\"?

 

 

SELECT 

 a.comment_id, 

 a.name, 

 a.email, 

 a.comment, 

 a.live, 

 b.article_name, 

 c.article_id, 

 c.comment_id, 

 c.commentjuke_id 

FROM 

 comments as a, 

 articles as b, 

 connect_commentarticles as c 

WHERE 

 b.article_id=\'$id\' 

 AND 

 c.article_id=b.article_id 

 AND 

 a.comment_id=c.comment_id 

 AND 

 a.live=\'1\'";

Link to comment
https://forums.phpfreaks.com/topic/812-3-tables-1-query/#findComment-2693
Share on other sites

After I set my query; here\'s what I have....

 

$result2 = mysql_query($query1,$db);

$num_results = mysql_num_rows($result2);

if($num_results!=0)

{

    do {



         // PRINT INFORMATION FROM DATABASE



         } while ($myrow2 = mysql_fetch_array($result2));

}

Link to comment
https://forums.phpfreaks.com/topic/812-3-tables-1-query/#findComment-2696
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.