tjhilder Posted December 23, 2005 Share Posted December 23, 2005 this is my question, would it be possible to count the numbers in a field in a table? like ... photo_album | photo_count (number of photos in the album) first album | 21 second album | 15 third album | 52 so that photo_count's contents are counted? 21 + 15 + 52 = 88 if possible, how would I do it? Link to comment https://forums.phpfreaks.com/topic/3091-counting-numbers-in-mysql-field/ Share on other sites More sharing options...
fenway Posted December 23, 2005 Share Posted December 23, 2005 You mean like using SUM()? Try the following: SELECT SUM(photo_count) FROM photos Hope that helps. Link to comment https://forums.phpfreaks.com/topic/3091-counting-numbers-in-mysql-field/#findComment-10333 Share on other sites More sharing options...
tjhilder Posted December 23, 2005 Author Share Posted December 23, 2005 ah thanks, I keep seeing that in places but when I wanted to use something like that it never came to mind lol how would I display that on the browser? Link to comment https://forums.phpfreaks.com/topic/3091-counting-numbers-in-mysql-field/#findComment-10346 Share on other sites More sharing options...
fenway Posted December 23, 2005 Share Posted December 23, 2005 I don't know what you mean by "on the browser" -- you retrieve this aggreate value in MySQL (you should give it a nice alias, like sum_count), and then get it with PHP, and shove it out to the browser the way you do with any other PHP script. Maybe I don't understand your question. Link to comment https://forums.phpfreaks.com/topic/3091-counting-numbers-in-mysql-field/#findComment-10355 Share on other sites More sharing options...
tjhilder Posted December 23, 2005 Author Share Posted December 23, 2005 this is the code I have (by what i thought you meant) $show_totals = "SELECT SUM(number) AS sum_count FROM gallery"; if ($r = mysql_query ($view_2003)) { $row = mysql_fetch_array($result); echo "{$row['sum_count']}"; } else { die ("Couldn\'t show the News because: <b>" . mysql_error() . "</b>. The query was $view_2003."); } I wanted to get the amount of photos from the gallery with MySQL and then echo them on the browser with PHP. sorry if that wasn't clear enough last time. -- TJ Link to comment https://forums.phpfreaks.com/topic/3091-counting-numbers-in-mysql-field/#findComment-10358 Share on other sites More sharing options...
fenway Posted December 24, 2005 Share Posted December 24, 2005 And why doesn't that work? Link to comment https://forums.phpfreaks.com/topic/3091-counting-numbers-in-mysql-field/#findComment-10361 Share on other sites More sharing options...
tjhilder Posted December 24, 2005 Author Share Posted December 24, 2005 [!--quoteo(post=330083:date=Dec 24 2005, 12:04 AM:name=fenway)--][div class=\'quotetop\']QUOTE(fenway @ Dec 24 2005, 12:04 AM) 330083[/snapback][/div][div class=\'quotemain\'][!--quotec--] And why doesn't that work? ok I got it working now, I didn't remember to change the variables, none of them where matching up properly, most likely cos i'm not well right now and can't think 100% lol so code now works cos it looks like this: $show_totals = "SELECT SUM(number) AS sum_count FROM gallery"; if ($r = mysql_query ($show_totals)) { $row = mysql_fetch_array($r); echo "{$row['sum_count']}"; } else { die ("Couldn\'t show total count of photos because: <b>" . mysql_error() . "</b>. The query was $show_totals."); } Link to comment https://forums.phpfreaks.com/topic/3091-counting-numbers-in-mysql-field/#findComment-10364 Share on other sites More sharing options...
ryanlwh Posted December 25, 2005 Share Posted December 25, 2005 you applied mysql_fetch_array on $result which sould be $r. Glad that you figured it out by yourself. Link to comment https://forums.phpfreaks.com/topic/3091-counting-numbers-in-mysql-field/#findComment-10397 Share on other sites More sharing options...
fenway Posted December 25, 2005 Share Posted December 25, 2005 I actually missed that, as well as the change in the query variable name, too! Didn't look that closely, I guess. Link to comment https://forums.phpfreaks.com/topic/3091-counting-numbers-in-mysql-field/#findComment-10402 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.