keefy Posted October 2, 2007 Share Posted October 2, 2007 Hi All...like the subject says im new to sql and i need a some help... I have on my website a comment script which is added to many pages for people to leave comments on certain restaurants in egypt. I want to place on another page how many comments have been left for each restaurant, just a simple Comments left = ? I have a table called c5t_comment, inside the table i have comment_id, comment_identifier_id, comment_identifier, comment_status plus more. All im intrested in is getting from table c5t_comment how many times in comment_identifier is = to /restaurants/hamees.php BUT comment_status = 0 the comment status if its = 100 it means its not been approved = 0 means it has been approved. Can anyone help ? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/71592-new-and-need-help-with-retrieving-data/ Share on other sites More sharing options...
keefy Posted October 3, 2007 Author Share Posted October 3, 2007 On the comment script forum, where no-one as posted for a long time someone had already asked the question i asked above and got this code:- <?php $sql = "SELECT count(*) as num FROM `c5t_comment` WHERE comment_identifier='/hotels/anglo.php"; $result = mysql_query($sql); $num_comments = mysql_result ($result,0,"num"); ?> <a href="/your/page/">Click here to see all <? echo $num ?> comments</a> however i get this error when i run it:- Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/fhlinux209/l/mysite.com/user/htdocs/restaurants/testc.php on line 13 Click here to see all comments line 13 is:- $num_comments = mysql_result ($result,0,"num"); Thanks for any help Quote Link to comment https://forums.phpfreaks.com/topic/71592-new-and-need-help-with-retrieving-data/#findComment-360613 Share on other sites More sharing options...
php_dave Posted October 3, 2007 Share Posted October 3, 2007 The Query is missing a ' after .php - so should read $sql = "SELECT count(*) as num FROM `c5t_comment` WHERE comment_identifier='/hotels/anglo.php'"; But is also dependant on the naming conventions of your table and fields - I would test the query in PHP myadmin or whatever DB front end you have just to make sure it returns a result - before you run in code. HTH Dave Quote Link to comment https://forums.phpfreaks.com/topic/71592-new-and-need-help-with-retrieving-data/#findComment-360651 Share on other sites More sharing options...
keefy Posted October 3, 2007 Author Share Posted October 3, 2007 Thanks for your reply php_dave, tried that in sql and it did not work:- MySQL said: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '$sql = "SELECT count(*) as num FROM `c5t_comment` WHERE comment_ i have MySQL 4.1.15-standard - phpMyAdmin 2.6.4-pl4 Quote Link to comment https://forums.phpfreaks.com/topic/71592-new-and-need-help-with-retrieving-data/#findComment-360986 Share on other sites More sharing options...
fenway Posted October 3, 2007 Share Posted October 3, 2007 You're passing the entire php line to mysql... there's an assignment! Quote Link to comment https://forums.phpfreaks.com/topic/71592-new-and-need-help-with-retrieving-data/#findComment-361039 Share on other sites More sharing options...
keefy Posted October 4, 2007 Author Share Posted October 4, 2007 Thanks all but since im very new to this and just started reading on it, is it possible 1 of you members can spare just a few mins and post me the full code what would be needed to fetch how many comments are left ? ive already learned how to connect to the database so im ok with that - im sure i will have hundreds more questions when i get myself more into php and sql. I need to get from table c5t_comment how many times in comment_identifier that /hotels/anglo.php is listed BUT comment_status must equal 0 i have MySQL 4.1.15-standard - phpMyAdmin 2.6.4-pl4 Thanks for your patience with me Keefy Quote Link to comment https://forums.phpfreaks.com/topic/71592-new-and-need-help-with-retrieving-data/#findComment-361534 Share on other sites More sharing options...
fenway Posted October 4, 2007 Share Posted October 4, 2007 SELECT count(*) as num FROM c5t_comment WHERE comment_identifier='/hotels/anglo.php' AND comment_status = 0 Quote Link to comment https://forums.phpfreaks.com/topic/71592-new-and-need-help-with-retrieving-data/#findComment-362030 Share on other sites More sharing options...
keefy Posted October 5, 2007 Author Share Posted October 5, 2007 Thanks fenway but im trying to write this in a php to show on my website, i thought it needed the $sql in the line ???? Im so sorry but i just cant get my head round it, i think that once i have the full code so that it shows on my website, then i will be able to understand it better. this is what i got from the comment script site which ive added to a php file to try and get it show the result on a webpage, but it does not work:- <?php require '_connect.php'; ?> //connect to the database <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title></title> </head> <body> <?php $sql = SELECT count(*) as num FROM "c5t_comment" WHERE comment_identifier='/hotels/anglo.php'"; $result = mysql_query($sql); $num_comments = mysql_result ($result,0,`num`); ?> <a href="/your/page/">Click here to see all <? echo $num_comments ?> comments</a> </body> </html> I hope im not annoying anyone in my persistence to understand this... Keefy Quote Link to comment https://forums.phpfreaks.com/topic/71592-new-and-need-help-with-retrieving-data/#findComment-362150 Share on other sites More sharing options...
fenway Posted October 6, 2007 Share Posted October 6, 2007 Assign that mysql statement to your php variable and you're done. Quote Link to comment https://forums.phpfreaks.com/topic/71592-new-and-need-help-with-retrieving-data/#findComment-363265 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.