dannyluked Posted July 29, 2009 Share Posted July 29, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/167961-query-inside-a-query/ Share on other sites More sharing options...
aschk Posted July 29, 2009 Share Posted July 29, 2009 You have a typo in your 2nd piece of code '$qry[title}' should be '$qry[title]' Quote Link to comment https://forums.phpfreaks.com/topic/167961-query-inside-a-query/#findComment-885897 Share on other sites More sharing options...
dannyluked Posted July 29, 2009 Author Share Posted July 29, 2009 oh yeah! That still isnt the problem though. That is actually just code i typed, not copied from my actual webpage! Quote Link to comment https://forums.phpfreaks.com/topic/167961-query-inside-a-query/#findComment-885904 Share on other sites More sharing options...
aschk Posted July 29, 2009 Share Posted July 29, 2009 Ok, so what error are you getting (echo mysql_error()) and what does $qry['title'] equal? Quote Link to comment https://forums.phpfreaks.com/topic/167961-query-inside-a-query/#findComment-885910 Share on other sites More sharing options...
dannyluked Posted July 29, 2009 Author Share Posted July 29, 2009 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' Quote Link to comment https://forums.phpfreaks.com/topic/167961-query-inside-a-query/#findComment-885917 Share on other sites More sharing options...
fenway Posted August 3, 2009 Share Posted August 3, 2009 Well, a table structure would help. Quote Link to comment https://forums.phpfreaks.com/topic/167961-query-inside-a-query/#findComment-889939 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.