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.

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'];

 

Link to post
Share on other sites
This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.