PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Fri Oct 20, 2017 12:46 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Mon Feb 13, 2017 2:47 pm 
Offline
Forum Newbie

Joined: Thu Jan 26, 2017 11:36 pm
Posts: 13
I am very thankful and grateful to this site and the moderators which have helped me alot i am still a beginner in php mysql
i will be very thankful if you could help in the following code
the error
Syntax: [ Download ] [ Hide ]
<?php


$thehost = 'localhost';
$dbname = 'tutorial';
$username = 'root';
$userpass = '';

$dbh = new PDO ("mysql:host = $thehost ; dbname = $dbname", $username, $userpass)
or die ("Error");
foreach($dbh->query('SELECT SUM(option1+option2) FROM feedback') as $row)
{
echo "<tr>";
echo "<td>" . $row['SUM(option1+option2)'] . "</td>";
echo "</tr>";
}
?>
 

Basically i want to add the values of two columns and print them
but the error i am facing is
Warning: Invalid argument supplied for foreach()
and neither its adding up
thanks alot in advance


Top
 Profile  
 
PostPosted: Tue Feb 14, 2017 2:03 am 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13434
Location: New York, NY, US
Syntax: [ Download ] [ Hide ]
$dbh = new PDO ("mysql:host = $thehost ; dbname = $dbname", $username, $userpass)
or die ("Error");
foreach($dbh->query('SELECT (SUM(option1)+SUM(option2)) AS total FROM feedback') as $row)
{
echo "<tr>";
echo "<td>" . $row['total'] . "</td>";
echo "</tr>";
}
 

Usually with SUM() you need to GROUP BY an appropriate column.

_________________
(#10850)


Top
 Profile  
 
PostPosted: Tue Feb 14, 2017 7:31 am 
Offline
Forum Newbie

Joined: Thu Jan 26, 2017 11:36 pm
Posts: 13
thanks for your prompt reply . i m facing this error when running the code
Warning: Invalid argument supplied for foreach()
thanks in advance


Top
 Profile  
 
PostPosted: Wed Feb 15, 2017 12:22 am 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13434
Location: New York, NY, US
See what $dbh->query() is returning:
Syntax: [ Download ] [ Hide ]
$dbh = new PDO ("mysql:host = $thehost ; dbname = $dbname", $username, $userpass)
or die ("Error");
echo "<pre>" . print_r($dbh->query('SELECT (SUM(option1)+SUM(option2)) AS total FROM feedback'), 1) . "</pre>";
 

_________________
(#10850)


Top
 Profile  
 
PostPosted: Sun Feb 26, 2017 10:41 am 
Offline
Forum Newbie

Joined: Thu Jan 26, 2017 11:36 pm
Posts: 13
thanks it did my work


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group