Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
KnowledgeWoods Consulting
5,607 PointsCalculate and echo Sum of a Column in MS SQL Database
My code is following but it is not working. Please help.
$sql="SELECT SUM(percentage) AS sum FROM completion_percent WHERE userid=1"
$result=$db->query($sql)->fetchColumn();
foreach($result as $total) {
echo $total['sum'];
}
3 Answers
Deoward Kotze
14,604 PointsHave you tried to run that query directly on the Database to see if it returns any results?
You might want to try something like this for the foreach
foreach($result as $key => $value) {
echo $value;
}
Hugo Paz
15,622 PointsWhat is it echoing out?
KnowledgeWoods Consulting
5,607 PointsNothing....
Hugo Paz
15,622 PointsHave you tried var_dump($result); before the foreach()
KnowledgeWoods Consulting
5,607 PointsThanks....Got it right now....!!!