Jump to content

Recommended Posts

This might be more of a mySQL question or a PHP question, I'm not exactly sure.  I have a field in my table (Deals) called "Price".  All I want is the sum of that field.

I tried using something like:

$price += $row['Price'];

This ended up as one long string, even though the database is set as a decimal number field.

Then I tried doing it from mySQL like this (from what I read), but I don't think I have the full answer:

foreach($stmt->query('SELECT SUM(Price) 
        FROM Deals') as $row) {
            echo "<tr>";
            echo "<td>Total</td>";
            echo "<td> ->".$row['SUM(Price)']."</td>";
            echo "</tr>";
        }

Anyway, if someone could just show me how to get that sum, I would greatly appreciate it because I am missing something here.

Share this post


Link to post
Share on other sites
5 minutes ago, benanamen said:

`

Uh... thanks benanamen.  Heheh.  

I kept searching, reading, and finally found the solution:

$stmt = $pdo->prepare("SELECT SUM(Price) AS sumprice FROM Deals");
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
echo $row['sumprice'];

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.