Page 1 of 1

Help wanted with PHP arithmetic and mySQL select

Posted: Wed Mar 10, 2004 5:24 am
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

Posted: Wed Mar 10, 2004 5:31 am
by AVATAr
i dont understand it very well... but you want to sum.

SELECT sum(that_int_column) from that_table

Almost there I think

Posted: Wed Mar 10, 2004 6:02 am
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

Posted: Wed Mar 10, 2004 6:08 am
by AVATAr
go to the manual http://www.php.net/mysql

Posted: Wed Mar 10, 2004 6:21 am
by J Reid
If I could make sense of the manual don't you think I would use it?

Posted: Wed Mar 10, 2004 6:25 am
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

Posted: Wed Mar 10, 2004 6:30 am
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

Posted: Wed Mar 10, 2004 6:32 am
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

Posted: Wed Mar 10, 2004 6:33 am
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

Posted: Wed Mar 10, 2004 6:33 am
by JayBird
always nice to post a solution to help others with the same problem in future ;)

Mark

Posted: Wed Mar 10, 2004 6:37 am
by AVATAr
Helping people to figure it out.

Posted: Wed Mar 10, 2004 6:44 am
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];
}