如何将一个数字加到另一个取自 mysql in php 的数字?

How to add one number to another taken from mysql in php?

问题来了: php

$stmt = $con->prepare('SELECT balance,balanceusd FROM accounts WHERE id = ?'); 
$stmt->bind_result($balance,$balanceusd,);

html部分

<?=$balanceusd + $balance?>

但是当我这样做时,它不会显示 2 个值相加。我可以使用 echo,但是这个页面有一些额外的内容要显示,所以这对我来说不实用。有没有办法在页面中添加这些数字?感谢您的帮助!

您需要调用 $stmt->execute()$stmt->fetch() 来执行查询并将结果行提取到变量中。

您也可以在查询中进行加法而不是 PHP。

$stmt = $con->prepare('SELECT balance + balanceusd AS total FROM accounts WHERE id = ?'); 
$stmt->bind_param("i", $userid);
$stmt->execute();
$stmt->bind_result($total);
$stmt->fetch();
<?= $total ?>