Page 1 of 1

basic: sum() only zeros

Posted: Wed Apr 06, 2005 12:37 am
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.

Posted: Wed Apr 06, 2005 12:43 am
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.

Posted: Wed Apr 06, 2005 1:06 am
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).

Posted: Wed Apr 06, 2005 1:22 am
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..

Posted: Wed Apr 06, 2005 1:40 am
by imstupid
hey thanks. it worked great.