Help wanted with PHP arithmetic and mySQL select

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
J Reid
Forum Newbie
Posts: 10
Joined: Wed Mar 10, 2004 5:24 am

Help wanted with PHP arithmetic and mySQL select

Post by J Reid »

Hi all,

I have written a photographic gallery script using mySQL and PHP. What I set out to do tonight was to query the DB and discover how many images in total are available for visitors to see.

There isn't a DB entry per image, rather I have a column showing how many images per gallery. For instance, a gallery of photograph on the Tokyo Fish Market has 15 images in it. Another on India has 18 etc etc.

What I wanted to do was select each of these values (the column data type in mySQL is INT) and total them. I thought it would be quite easy but I must admit to now being stumped.

Any help greatly appreciated.

Jon
User avatar
AVATAr
Forum Regular
Posts: 524
Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:

Post by AVATAr »

i dont understand it very well... but you want to sum.

SELECT sum(that_int_column) from that_table
J Reid
Forum Newbie
Posts: 10
Joined: Wed Mar 10, 2004 5:24 am

Almost there I think

Post by J Reid »

Probably sounds stupid, but now I am getting Resource ID#9 after running your SELECT suggestion..
How do I turn the fetched result into a usable variable?

Jon
User avatar
AVATAr
Forum Regular
Posts: 524
Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:

Post by AVATAr »

go to the manual http://www.php.net/mysql
J Reid
Forum Newbie
Posts: 10
Joined: Wed Mar 10, 2004 5:24 am

Post by J Reid »

If I could make sense of the manual don't you think I would use it?
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

J Reid wrote:If I could make sense of the manual don't you think I would use it?
Yeah, you'd think wouldn't you! Most people don't though, and usually don't even try.

Can you show us your code with the example that Avatar gave you.

Mark
User avatar
AVATAr
Forum Regular
Posts: 524
Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:

Post by AVATAr »

One of the best resources to learn is to hack the Example codes that are in the manual... And the contribution notes of the user in the bottom.

check them.. you'll figure it out.. if not, show us your code.. but i see you have to use http://www.php.net/manual/en/function.m ... -array.php
J Reid
Forum Newbie
Posts: 10
Joined: Wed Mar 10, 2004 5:24 am

Post by J Reid »

I have the manual, all 400 odd pages, in PDF open here in front of me... but like I said...

Was about to post the code but I figured it out, no thanks to the manual, but thanks to you and Avatar for taking the time.......

Jon
J Reid
Forum Newbie
Posts: 10
Joined: Wed Mar 10, 2004 5:24 am

Post by J Reid »

I have the manual, all 4000 odd pages, in PDF open here in front of me... but like I said...

Was about to post the code but I figured it out, no thanks to the manual, but thanks to you and Avatar for taking the time.......

Jon
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

always nice to post a solution to help others with the same problem in future ;)

Mark
User avatar
AVATAr
Forum Regular
Posts: 524
Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:

Post by AVATAr »

Helping people to figure it out.
J Reid
Forum Newbie
Posts: 10
Joined: Wed Mar 10, 2004 5:24 am

Post by J Reid »

This is my final working code:
$pics = mysql_query("SELECT sum(gallery_pics) FROM sn_galleries");
while ($row = mysql_fetch_array($pics)) {
$total = $row[0];
}
Post Reply