delphi123 Posted January 31, 2008 Share Posted January 31, 2008 Hi I'm wanting to have a php page that displays the number of posts by a person. There's a column called author in my database that holds this value, I'm wondering how I can do this? Is there some way of counting the number of rows in a database that have a column with value x? Link to comment https://forums.phpfreaks.com/topic/88727-solved-count-if-column-value-x/ Share on other sites More sharing options...
laffin Posted January 31, 2008 Share Posted January 31, 2008 SELECT COUNT(*) FROM posts WHERE user='x'; Link to comment https://forums.phpfreaks.com/topic/88727-solved-count-if-column-value-x/#findComment-454416 Share on other sites More sharing options...
delphi123 Posted January 31, 2008 Author Share Posted January 31, 2008 brilliant cheers! (easier than I was expecting!) Link to comment https://forums.phpfreaks.com/topic/88727-solved-count-if-column-value-x/#findComment-454421 Share on other sites More sharing options...
delphi123 Posted January 31, 2008 Author Share Posted January 31, 2008 hmmm - stupid additional question - how do I now print this value? I've tried echo $result after my query but it just says resource id #14?! Link to comment https://forums.phpfreaks.com/topic/88727-solved-count-if-column-value-x/#findComment-454424 Share on other sites More sharing options...
haku Posted January 31, 2008 Share Posted January 31, 2008 $result = mysql_fetch_array($result) echo $result[0]; Link to comment https://forums.phpfreaks.com/topic/88727-solved-count-if-column-value-x/#findComment-454430 Share on other sites More sharing options...
delphi123 Posted January 31, 2008 Author Share Posted January 31, 2008 oh I feel stupid! Link to comment https://forums.phpfreaks.com/topic/88727-solved-count-if-column-value-x/#findComment-454434 Share on other sites More sharing options...
laffin Posted January 31, 2008 Share Posted January 31, 2008 if($res=mysql_query("SELECT COUNT(*) FROM posts WHERE user='x';")) { $row=mysql_result($res,0); } else $posts=0; shud do it Link to comment https://forums.phpfreaks.com/topic/88727-solved-count-if-column-value-x/#findComment-454436 Share on other sites More sharing options...
delphi123 Posted January 31, 2008 Author Share Posted January 31, 2008 thanks Laffin - that's actually really useful - the way you did it is not something I'd thought of, so my code can be much neater now! Is it possible to 'stack' the where statements? Ie I'm now looking to show the posts that person x has posted in the last week, so I'd need something like: count(x) where author='x' and where date='less than 7 days from current time' Link to comment https://forums.phpfreaks.com/topic/88727-solved-count-if-column-value-x/#findComment-454447 Share on other sites More sharing options...
laffin Posted January 31, 2008 Share Posted January 31, 2008 SELECT COUNT(*) FROM posts WHERE DATE_SUB(CURDATE(),INTERVAL 7 DAY) <= date_field; Link to comment https://forums.phpfreaks.com/topic/88727-solved-count-if-column-value-x/#findComment-454460 Share on other sites More sharing options...
delphi123 Posted January 31, 2008 Author Share Posted January 31, 2008 brilliant - thanks so much Laffin, you've been a great help with this! Link to comment https://forums.phpfreaks.com/topic/88727-solved-count-if-column-value-x/#findComment-454470 Share on other sites More sharing options...
laffin Posted January 31, 2008 Share Posted January 31, 2008 No probs, hope ya gets yer project going Link to comment https://forums.phpfreaks.com/topic/88727-solved-count-if-column-value-x/#findComment-454502 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.