计算总价值的百分比
Calculating Percentage of total value
好的,我想知道总行数中有多少百分比是正确的(结果=1)结果 2 意味着它仍然有效,所以只需要结果!= 2
的总行数
我有以下代码,但没有显示任何内容
我做错了什么(公平地说我还睡着了一半)
$successq = mysqli_query($con,"SELECT COUNT(*) FROM `table`
WHERE `result` ='1'");
$success = mysqli_fetch_assoc($successq);
$totalq = mysqli_query($con,"SELECT COUNT(*) FROM `table`
WHERE `result` !='2'");
$total = mysqli_fetch_assoc($totalq);
$percent = ($success/$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_fetch_assoc()
return 关联数组,所以不能作为多变的。您必须使用它的关联索引从中获取值(就像我在 echo $percent = ($success['success'] /$total['total'] ) * 100;
中所做的那样)。
因为mysqli_fetch_assoc
return数组不是单值。所以改变你的代码如下:
$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;
好的,我想知道总行数中有多少百分比是正确的(结果=1)结果 2 意味着它仍然有效,所以只需要结果!= 2
的总行数我有以下代码,但没有显示任何内容 我做错了什么(公平地说我还睡着了一半)
$successq = mysqli_query($con,"SELECT COUNT(*) FROM `table`
WHERE `result` ='1'");
$success = mysqli_fetch_assoc($successq);
$totalq = mysqli_query($con,"SELECT COUNT(*) FROM `table`
WHERE `result` !='2'");
$total = mysqli_fetch_assoc($totalq);
$percent = ($success/$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_fetch_assoc()
return 关联数组,所以不能作为多变的。您必须使用它的关联索引从中获取值(就像我在 echo $percent = ($success['success'] /$total['total'] ) * 100;
中所做的那样)。
因为mysqli_fetch_assoc
return数组不是单值。所以改变你的代码如下:
$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;