Jump to content

Query inside a query...


dannyluked

Recommended Posts

MySQL version = 5.1

Hi,

I want to add a comments section to my homemade blog! Firstly I currently have a blog (that queries a database) on my site homepage. The initial blog links to another page that then shows the full blog (links to mysite.com/view_blog?id=(Query ID)). I now want users to be able to post comments under the full blog and on the initial page to show how many comments there are.

 

The idea I am currently trying is that I have created a table called 'blogcomments'. In that table the blog title that you are commenting on is added and the comment. I want the initial blog to do the following:

Query the normal blog things (title, date, writing, links...) but also count the number of comments for that particular blog title.

I so far have:

<?php
   include "config.php";
     
   mysql_connect($server, $db_user, $db_pass) or die (mysql_error());
   
   $result = mysql_db_query($database, "select * from $table order by id desc LIMIT 3") or die (mysql_error());
   while ($qry = mysql_fetch_array($result)) {       echo "<div id='title'>$qry[title]</div><div id='date'>$qry[date]</div><dive id='comments'>I WANT TO DISPLAY NUMBER OF COMMENTS HERE</div> "; 
   } 
?>

 

Idearly I would have

      $count = mysql_db_query($database, "SELECT * FROM blogcomments where blogtitle = '$qry[title}'") or die (mysql_error());
                $num_rows = mysql_num_rows($count);

but this dosent work!

 

So my question is,

How do I query the number of comments that have the title $qry[title] baring in mind the comments are in another table.

Sorry if it is hard to understand.

Link to comment
Share on other sites

I think I need to join queries or something but i havent done that before. I dont get any errors just no results! I want to query the table 'blogcomments' but use a WHERE clause. But in the WHERE clause I want to query a table called 'blog'! But overall I want it to count the rows in 'blogcomments' that have a title form 'blog'

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.