Jump to content


Photo

counting numbers in mysql field?


  • Please log in to reply
8 replies to this topic

#1 tjhilder

tjhilder
  • Members
  • PipPipPip
  • Advanced Member
  • 136 posts
  • LocationKent, UK

Posted 23 December 2005 - 08:00 PM

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?

#2 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 23 December 2005 - 08:58 PM

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

SELECT SUM(photo_count) FROM photos
Hope that helps.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#3 tjhilder

tjhilder
  • Members
  • PipPipPip
  • Advanced Member
  • 136 posts
  • LocationKent, UK

Posted 23 December 2005 - 10:28 PM

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?

#4 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 23 December 2005 - 11:30 PM

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.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#5 tjhilder

tjhilder
  • Members
  • PipPipPip
  • Advanced Member
  • 136 posts
  • LocationKent, UK

Posted 23 December 2005 - 11:38 PM

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

#6 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 24 December 2005 - 12:04 AM

And why doesn't that work?
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#7 tjhilder

tjhilder
  • Members
  • PipPipPip
  • Advanced Member
  • 136 posts
  • LocationKent, UK

Posted 24 December 2005 - 12:09 AM

[!--quoteo(post=330083:date=Dec 24 2005, 12:04 AM:name=fenway)--][div class=\'quotetop\']QUOTE(fenway @ Dec 24 2005, 12:04 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
And why doesn't that work?
[/quote]
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.");
}


#8 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 25 December 2005 - 07:27 PM

you applied mysql_fetch_array on $result which sould be $r. Glad that you figured it out by yourself.
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#9 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 25 December 2005 - 11:01 PM

I actually missed that, as well as the change in the query variable name, too! Didn't look that closely, I guess.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users