Jump to content

Adding


TapeGun007

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 comment
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 comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.