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 關注
- 146 瀏覽
添加回答
舉報
