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

PHP

Calculate 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

Have 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; }

What is it echoing out?

Nothing....

Have you tried var_dump($result); before the foreach()

Thanks....Got it right now....!!!