Jump to content

Archived

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

jabbamonkey

3 tables, 1 query

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!

Share this post


Link to post
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\'";

Share this post


Link to post
Share on other sites

Looks good. You only need 1 articleID in the SELECT clause BTW (not that it matters, but its efficient syntax).

Share this post


Link to post
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\'";

Share this post


Link to post
Share on other sites

No, the order of the WHERE clauses does not matter.

 

What are you passing to mysql_num_rows?

 

I just tested your syntax with one of my a-b-c tables and it worked.

Share this post


Link to post
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));

}

Share this post


Link to post
Share on other sites

Best way is to

 

echo \"$query1\"

 

to see what is actually being executed. Could be that $id is empty.

Share this post


Link to post
Share on other sites

×

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.