php中mysql列乘以变量的和

waxmsbnn  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(487)

我是一个爱好开发人员,我遇到了一个障碍,我已经试着解决了几天了,所以我首先要感谢任何能帮助我的人。
我试图通过php显示以下结果。
(nb-历史积分转换为1万美元)

$get_earned = mysqli_query($conn, "SELECT SUM(history_points) FROM activity_history") or die(mysql_error());

while($row = mysqli_fetch_array($get_earned)){
$total_points = $row['SUM(history_points)'];

结果是

SUM ( `history_points`)
 218903.0000

然后,我使用以下方式显示此结果:

php echo "$".convert(number_format($total_points));

现在问题是它显示为 $0.0218 什么时候应该读 $21.89 我尝试了下面的方法,它显示$0,其中10000是点到美元的转换。

$get_earned = mysqli_query($conn, "SELECT SUM(history_points * 10000) FROM activity_history") or die(mysql_error());

我完全不知所措。

7gs2gvoe

7gs2gvoe1#

试试这个:

$get_earned = mysqli_query ( $conn, "SELECT SUM(history_points) FROM activity_history" ) or die ( mysql_error () );
$total_points = 0;
if ($get_earned !== false) {
    if ($row = mysqli_fetch_assoc ( $get_earned )) {
        $total_points = $row [0];
    }
}

// If you need to multiply the total by a variable in php, then simply multiply it:
if ($total_points !== 0) {
    $constant_var = 7;//just an example, it's ur constant
    echo "$" . number_format ( $constant_var * $total_points, 2 );
}
cngwdvgl

cngwdvgl2#

将查询更改为:

"SELECT SUM(history_points * 10000) FROM activity_history"

列名也会更改,因此必须在php中获取正确的值。

$total_points = $row['SUM(history_points * 10000)'];

根据@pritaeas的建议,最好使用别名。

"SELECT SUM(history_points * 10000) as ABC FROM activity_history"

$total_points = $row['ABC'];

相关问题