Skip to content

Instantly share code, notes, and snippets.

@davidchc
Last active January 2, 2016 07:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidchc/8273759 to your computer and use it in GitHub Desktop.
Save davidchc/8273759 to your computer and use it in GitHub Desktop.
<?php
$pdo = new PDO("mysql:host=localhost;dbname=vendas", "root", "");
$stmt = $pdo->prepare("SELECT * FROM produtos");
$stmt->execute();
$rows = $stmt->fetchAll(PDO:FETCH_ASSOC);
$total = 0;
foreach($rows as $produto){
$total += $produto['preco'];
}
<?php
$pdo = new PDO("mysql:host=localhost;dbname=vendas", "root", "");
$sql = "SELECT SUM(preco) AS total FROM produtos";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$row = $stmt->fetch(PDO:FETCH_ASSOC);
echo "Total ".$row['total'];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment