BadGoat Posted September 25, 2006 Share Posted September 25, 2006 Hello!I am trying to echo the sum of the records in a table and it returns empty. [code]$query = "SELECT SUM(number) AS total FROM records"; $result = mysql_query($query); echo 'the total is:'; echo $total;[/code]When I echo the query it appears as above, when I echo the $result it says Resource ID#5. When I echo $total is is empty. What am I doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/22024-sum-question-solved/ Share on other sites More sharing options...
eric1235711 Posted September 25, 2006 Share Posted September 25, 2006 $result content is the resource id...you must fetch to get somethingand also, php won´t guess what is $total...try this:[code]<?php$query = "SELECT SUM(number) AS total FROM records"; $result = mysql_query($query); list($total) = mysql_fetch_row($result); // look at php.net to learn more about this echo 'the total is:'; echo $total;?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/22024-sum-question-solved/#findComment-98485 Share on other sites More sharing options...
.josh Posted September 25, 2006 Share Posted September 25, 2006 [code]$query = "SELECT SUM(number) AS total FROM records"; $result = mysql_query($query); $r = mysql_fetch_array($result); echo "total is: {$r['total']}";[/code] Quote Link to comment https://forums.phpfreaks.com/topic/22024-sum-question-solved/#findComment-98486 Share on other sites More sharing options...
ober Posted September 25, 2006 Share Posted September 25, 2006 [code]$query = "SELECT SUM(number) AS total FROM records";$result = mysql_query($query);$row = mysql_fetch_assoc($result);echo 'the total is:' . $row['total'];[/code]Try that.EDIT:... bah... beaten to it. Quote Link to comment https://forums.phpfreaks.com/topic/22024-sum-question-solved/#findComment-98487 Share on other sites More sharing options...
BadGoat Posted September 25, 2006 Author Share Posted September 25, 2006 Happily I asked a question that three people answered within 10 minutes! Many thanks all! Quote Link to comment https://forums.phpfreaks.com/topic/22024-sum-question-solved/#findComment-98493 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.