php中mysql列乘以变量的总和
我是一个爱好开发人员,我遇到了一个困难,几天来我一直在努力解决这个问题,所以我首先要感谢任何能提供帮助的人 我试图通过php显示以下结果 (注意-历史积分转换为10000美元) 结果是php中mysql列乘以变量的总和,php,mysql,Php,Mysql,我是一个爱好开发人员,我遇到了一个困难,几天来我一直在努力解决这个问题,所以我首先要感谢任何能提供帮助的人 我试图通过php显示以下结果 (注意-历史积分转换为10000美元) 结果是 SUM ( `history_points`) 218903.0000 然后,我使用以下命令显示此结果: php echo "$".convert(number_format($total_points)); 现在的问题是,当它应该读取$21.89 我尝试了以下方法,其中显示$0,其中10000是点到美
SUM ( `history_points`)
218903.0000
然后,我使用以下命令显示此结果:
php echo "$".convert(number_format($total_points));
现在的问题是,当它应该读取$21.89
我尝试了以下方法,其中显示$0,其中10000是点到美元的转换
$get_earned = mysqli_query($conn, "SELECT SUM(history_points * 10000) FROM activity_history") or die(mysql_error());
我完全不知所措。当您将查询更改为:
"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'];
当您将查询更改为:
"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'];
试试这个:
$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 );
}
试试这个:
$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 );
}
什么是
convert()
?mysqli\u query()
不适用于mysql\u error()
。不同的API。你在混合mysql API。如果您遇到无法工作的错误。number\u格式(218903.0000)
将导致218903
。因此,可能是您神秘的convert
函数把事情搞砸了。什么是convert()
?mysqli\u query()
不适用于mysql\u error()
。不同的API。你在混合mysql API。如果您遇到无法工作的错误。number\u格式(218903.0000)
将导致218903
。因此,可能是您神秘的convert
函数弄乱了这一点。或者使用别名:SELECT SUM(history\u points*10000)作为history\u points\u SUM FROM activity\u history
Yes。那会更好。更新了答案。我尝试了此解决方案并收到Http错误500,我尝试删除.convert(number_格式)部分,如上所述,我还删除了所有mysql,只留下mysqli(我没有编写此脚本,我正在尝试修复它:p)或使用别名:SELECT SUM(history_points*10000)作为历史记录\u点\u与活动\u历史记录之和
是。那会更好。更新了答案。我尝试了此解决方案并收到Http错误500,我尝试删除.convert(number_格式)部分,如上所述,我还删除了所有mysql,只留下了mysqli(我没有编写此脚本,我正在尝试修复它:p)尝试显示它而不使用number_格式,echo“$”。($constant_var*$total_points)代码>并且您确定从数据库获得的总和(历史点)正确吗?您是如何知道它是218903.0000的?我们需要确保您从查询中得到正确的结果,以显示它,而不使用数字格式,echo“$”。($constant_var*$total_points)代码>并且您确定从数据库获得的总和(历史点)正确吗?您怎么知道是218903.0000?我们需要确保您从查询中得到正确的结果