Jump to content

Archived

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

tjhilder

counting numbers in mysql field?

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?

Share this post


Link to post
Share on other sites

You mean like using SUM()? Try the following:

 

SELECT SUM(photo_count) FROM photos

Hope that helps.

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

And why doesn't that work?

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

you applied mysql_fetch_array on $result which sould be $r. Glad that you figured it out by yourself.

Share this post


Link to post
Share on other sites

I actually missed that, as well as the change in the query variable name, too! Didn't look that closely, I guess.

Share this post


Link to post
Share on other sites

×

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.