Undefined index error?

They have: 27 posts

Joined: Oct 2001

Anyone know why i get this error?

Notice: Undefined index: totamt

Here's the code.

<?php
    $db_query_sum
= db_query(\"SELECT 'totamt' = sum(amt) from payment WHERE prgname='\".$db_values[\"prgname\"].\"'\");
$db_values_amt = mysql_fetch_array($db_query_sum);
echo
$db_values[\"prgname\"].\" - \".$db_values_amt[\"totamt\"].\" - \". $db_query_sum.\"<br>\";
?>

The $db_values["prgname"] line is from another sql database result.

Thanks

Keith

They have: 27 posts

Joined: Oct 2001

Just got it to work.

Thanks anyway

Keith

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

Perhaps post up the solution so that others may learn from this?

They have: 27 posts

Joined: Oct 2001

Ok ummm...

I couldnt get it to work by referencing the 'totamt' or 'amt', not sure why? Here is how i got it working.

<?php
$db_query_sum
= db_query(\"SELECT sum(amt) from payment WHERE prgname='\".$db_values[\"prgname\"].\"'\");
$db_values_amt = mysql_fetch_array($db_query_sum);
echo
$db_values[\"prgname\"].\" - \".$db_values_amt[\"sum(amt)\"].\"<br>\";
?>

Keith

Busy's picture

He has: 6,151 posts

Joined: May 2001

select sum(amt) as totamt ... ?

CptAwesome's picture

He has: 370 posts

Joined: Dec 2004

Keith, since you're not sure why, I think I can clarify.

SELECT selects a column from the row which is found with the where clause.

I am guessing that it found multiple columns, and the sum() adds them together. Which is why amt wouldn't work, because it's going to find multiple rows, and you didn't loop your mysql_fetch_array

$db_query_sum = db_query("SELECT amt FROM payment WHERE prgname='".$db_values["prgname"]."'");
while($db_loop_value = mysql_fetch_array($db_query_sum))
{
$db_values_amt[] = $db_loop_value[amt];
}
$totam = array_sum($db_values_amt);
echo $db_values["prgname"]." - ".$db_values_amt["totamt"]." - ". $db_query_sum."<br>";
'

CptAwesome's picture

He has: 370 posts

Joined: Dec 2004

CptAwesome wrote: I am guessing that it found multiple columns, and the sum() adds them together. Which is why amt wouldn't work, because it's going to find multiple rows, and you didn't loop your mysql_fetch_array

should have been "I am guessing that it found multiple rows"

the code I showed would pull the amt column into an array, and then array_sum would add them together.

and I didn't try what Busy said, but "X as Y" might work, I am pretty new at MySQL mySelf Smiling

They have: 27 posts

Joined: Oct 2001

Well the column name is 'amt', and their is only one, so it shouldnt have gotten multiple columns. What i wanted to do add all of the info in the 'amt' column. So the sum() function takes all of the rows and adds the amt column. Well i didnt know how to reference this value, so i tried to name it totamnt, but i couldnt reference totamt(dont know why). So in order to reference the sum i had to use 'sum(amt)'.

Keith

They have: 27 posts

Joined: Oct 2001

Just tried what Busy said. Didnt work Sad

Keith

Busy's picture

He has: 6,151 posts

Joined: May 2001

try count(amt) as totamt

looking at your orginal code

Quote: $db_query_sum = db_query("SELECT 'totamt' = sum(amt) from payment WHERE prgname='".$db_values["prgname"]."'");
$db_values_amt = mysql_fetch_array($db_query_sum);
echo $db_values["prgname"]." - ".$db_values_amt["totamt"]." - ". $db_query_sum."";

you are only calling a value, not an array, so the $db_values_amt line isn't needed.

try something like:
$db_query = "select count(amt) as totamt from payment WHERE prgname='".$db_values["prgname"]."'";
$db_result = mysql_query($db_query);
$totamt = mysql_result($db_result,0,"totamt");

I didn't test the above but should work.
depending what amt's value is, you could possibly use max() if you're just trying to find the largest number. count will add them up, max will give largest number, sum will add them up (as in dollar value), think there are a couple more as well

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.