basic: sum() only zeros

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
imstupid
Forum Commoner
Posts: 84
Joined: Fri Feb 18, 2005 1:24 pm

basic: sum() only zeros

Post by imstupid »

Hey everybody-
thanks in advance for all the help lately. Here's the situation. I have a table, with a column named "minutes." The fields are set as integer, not null, default value of 0. I am using mysql_query sum() to add all values of that column together obviously. The column is filled with only zeros, and when I try to run the sum() query, php gives me the blank screen, no error messages or anything. If I add in a value of 1, 2, 3...whatever to that column via html form, it works fine. but if the column is filled with all zeros, it doesn't work. Hypothetically, I need the column to stay as all zeros until the user changes it. On the code below, assume all db_connect code, etc. is applied...

Code: Select all

<?php
$getallminutes = mysql_query("SELECT SUM(minutes) AS totalminutes FROM table_1 WHERE name='$name'") 
 		or die(mysql_error()); 
	$currentminutes = mysql_result($getallminutes, minutes, 0)
		or die(mysql_error());
<?
Thanks again.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

  1. your query requests the field stored as "totalminutes"
  2. remember to quote your string literals. i.e. 'minutes' on your mysql_result() line.
imstupid
Forum Commoner
Posts: 84
Joined: Fri Feb 18, 2005 1:24 pm

Post by imstupid »

thanks for the fast reply. Do I need to remove that "AS totalminutes" section of my mysql_query altogether? I have this now and it still isn't working:

Code: Select all

<?PHP
$getallminutes = mysql_query("SELECT SUM(minutes) AS totalminutes FROM table_1 WHERE name='$name'") 
 		or die(mysql_error()); 
	$currentminutes = mysql_result($getallminutes, 'minutes', 0)
		or die(mysql_error());
?>
I need that puppy to not only print/echo a 0, but also to insert a 0 into another table once it is working. sorry. By the way, what is that last 0 for in the mysql_result line? I was trying to find that out on the php.net manual, but that site is hard to read after a few cans of busch light.(it was on sale).
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the arguments to mysql_result are:
  1. the result resource returned by mysql_query()
  2. the row you wish to get
  3. optionally, the field you want from that row
so, your second argument (minutes) needs to swap places with the third, and it needs to change to 'totalminutes' :|

also, if mysql_result() does return a zero, the die() will run. So you may be a blank page..
imstupid
Forum Commoner
Posts: 84
Joined: Fri Feb 18, 2005 1:24 pm

Post by imstupid »

hey thanks. it worked great.
Post Reply