Php 计算总价值的百分比
好的,我想知道行总数中正确的百分比是多少(result=1)。result 2表示它仍然处于活动状态,所以只需要result!=二, 我有以下代码,但没有显示任何内容 我做错了什么(公平地说,我仍有一半是清醒的)Php 计算总价值的百分比,php,math,mysqli,percentage,Php,Math,Mysqli,Percentage,好的,我想知道行总数中正确的百分比是多少(result=1)。result 2表示它仍然处于活动状态,所以只需要result!=二, 我有以下代码,但没有显示任何内容 我做错了什么(公平地说,我仍有一半是清醒的) 您需要为COUNT(*)添加别名,然后需要使用该别名来获取值 您需要echo您的最终输出 你可以这样做:- $successq = mysqli_query($con,"SELECT COUNT(*) as success FROM `table` WHERE `result` ='
您需要为
COUNT(*)
添加别名,然后需要使用该别名来获取值
您需要echo
您的最终输出
你可以这样做:-
$successq = mysqli_query($con,"SELECT COUNT(*) as success FROM `table`
WHERE `result` ='1'");//success used as alias
$success = mysqli_fetch_assoc($successq);
$totalq = mysqli_query($con,"SELECT COUNT(*) as total FROM `table`
WHERE `result` !='2'");// total use as alias
$total = mysqli_fetch_assoc($totalq);
echo $percent = ($success['success'] /$total['total'] ) * 100;// use aliases and echo the output
注意:-
mysqli\u fetch\u assoc()
返回关联数组,因此不能将其用作变量。您必须使用它的关联索引从中获取值(就像我在echo$percent=($success['success']/$total['total'])*100;
)。您需要为COUNT(*)
添加别名,然后您需要使用该别名来获取值
您需要echo
您的最终输出
你可以这样做:-
$successq = mysqli_query($con,"SELECT COUNT(*) as success FROM `table`
WHERE `result` ='1'");//success used as alias
$success = mysqli_fetch_assoc($successq);
$totalq = mysqli_query($con,"SELECT COUNT(*) as total FROM `table`
WHERE `result` !='2'");// total use as alias
$total = mysqli_fetch_assoc($totalq);
echo $percent = ($success['success'] /$total['total'] ) * 100;// use aliases and echo the output
注意:-
mysqli\u fetch\u assoc()
返回关联数组,因此不能将其用作变量。您必须使用它的关联索引从中获取值(就像我在echo$percent=($success['success']/$total['total'])*100;
)。因为mysqli\u fetch\u assoc
返回数组不是单个值。因此,请按以下方式更改代码:
$successq = mysqli_query($con,"SELECT COUNT(*) as total FROM `table`
WHERE `result` ='1'");
$success = mysqli_fetch_assoc($successq);
$success_cnt = $success["total"];
$totalq = mysqli_query($con,"SELECT COUNT(*) as total FROM `table`
WHERE `result` !='2'");
$total = mysqli_fetch_assoc($totalq);
$total_cnt = $total["total"];
$percent = ($success_cnt /$total_cnt ) * 100;
因为
mysqli\u fetch\u assoc
返回数组不是单个值。因此,请按以下方式更改代码:
$successq = mysqli_query($con,"SELECT COUNT(*) as total FROM `table`
WHERE `result` ='1'");
$success = mysqli_fetch_assoc($successq);
$success_cnt = $success["total"];
$totalq = mysqli_query($con,"SELECT COUNT(*) as total FROM `table`
WHERE `result` !='2'");
$total = mysqli_fetch_assoc($totalq);
$total_cnt = $total["total"];
$percent = ($success_cnt /$total_cnt ) * 100;
再次感谢你,我知道我不是很傻spotting@ChrisYates很乐意帮忙。请仔细阅读php手册。你会明白自己做错了什么。再次感谢你,我知道我不是在做傻事spotting@ChrisYates很乐意帮忙。请仔细阅读php手册。你会知道自己做错了什么。