Forum Moderators: coopster

Message Too Old, No Replies

problem with Sum function

         

hiedler

2:03 pm on Apr 8, 2008 (gmt 0)

10+ Year Member



hi
i want to make a counter for my web page, i created table,where I count all acces from different IPīs to my site in column PRISTUPY (table POCITADLO)...
but there is a problem when i want to SUM this column:
i use it like this:
$sql= "SELECT sum(pristupu) as total FROM pocitadlo";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
echo $row['total'];

but on my site ,instead of counter,there is written this
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\...

anyone who knows ,what sholud I do?

PHP_Chimp

3:20 pm on Apr 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Are you sure that there isnt a problem with the SELECT statement?
Try -

$sql= "SELECT sum(pristupu) as total FROM pocitadlo";
$result = mysql_query($sql);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
else {
$row = mysql_fetch_array($result);
echo $row['total'];
}

hiedler

4:07 pm on Apr 8, 2008 (gmt 0)

10+ Year Member



yeah,problem is there...really thanks for the function mysql_error() it showed me,that the problem was because,i wasnt correctly connected to my dabase...
thanks a lot PHP_Chimp