mysql列的总和乘以php中的变量

时间:2018-05-16 12:13:55

标签: php mysql

我是业余爱好开发者,而且我已经遇到了几天我试图解决的障碍,所以我首先要感谢任何可以提供帮助的人。< / p>

我试图通过php显示以下结果。

  

(nb - history_points的转换为10,000到$ 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是usd转换的点。

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

我不知所措。

2 个答案:

答案 0 :(得分:2)

当您将查询更改为:

"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'];

答案 1 :(得分:0)

试试这个:

$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 );
}