2 回答
TA贡献2011条经验 获得超2个赞
获取结果集后,您将获得一个结果数组 - 这不能作为echo. 您需要选择要打印的确切列。如果你为它分配一个别名,那就更容易了SUM(total) AS tot——现在总和的名称是tot.
还可以在准备好的语句中使用适当的有界参数。
$stmt4 = $dbcon->prepare("SELECT SUM(total) as tot FROM transactions WHERE proid=?");
$stmt4->execute([$id]);
$thetotal = $stmt4->fetch(PDO::FETCH_ASSOC);
echo $thetotal['tot'];
TA贡献1830条经验 获得超3个赞
警告切勿将 PHP 变量放入 SQL 字符串中。它使您的 SQL 容易受到 SQL 注入的影响,并可能导致您遇到很多问题。
您可以通过使用解决您的问题 fetchColumn
$stmt4 = $dbcon->prepare("SELECT SUM(total) FROM transactions WHERE proid=? ");
$stmt4->execute([$id]);
$thetotal = $stmt4->fetchColumn();
echo $thetotal;
或者您可以使用fetch与PDO::FETCH_COLUMN
$stmt4 = $dbcon->prepare("SELECT SUM(total) FROM transactions WHERE proid=? ");
$stmt4->execute([$id]);
$thetotal = $stmt4->fetch(PDO::FETCH_COLUMN);
echo $thetotal;
- 2 回答
- 0 关注
- 124 浏览
添加回答
举报