Jump to content

counting numbers in mysql field?


tjhilder

Recommended Posts

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

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.

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

[!--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.");
}

Archived

This topic is now archived and is closed to further replies.

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