下面的代码在显示mysql的值时在屏幕上显示INF和NAN值。我在mysql中运行查询,我能够获得所需的值,但是在返回值的网页上却得到了上述值。
function get_preelections_results_public(){
global $mysqli;
$return_data = "";
$squery = "SELECT 'PreElection', (SELECT count(pu_name) from polling_unit) pu_count, (SELECT count( distinct pu_name) from pre_election_check_info) pu_reported_count,(SELECT count(distinct pu_name) from pre_election_check_info a WHERE inec_arrival_status='Yes' and security_arrival_status='Yes' and entry_date = (select max(entry_date) from pre_election_check_info b where a.pu_name=b.pu_name)) green_count,
(SELECT count(distinct pu_name) from pre_election_check_info a WHERE inec_arrival_status ='Yes' and security_arrival_status ='No' and entry_date =
(select max(entry_date) from pre_election_check_info b where a.pu_name=b.pu_name)) amber_count_1, (SELECT count(distinct pu_name) from pre_election_check_info a WHERE inec_arrival_status ='No' and security_arrival_status ='Yes' and entry_date = (select max(entry_date) from pre_election_check_info b where a.pu_name=b.pu_name)) amber_count_2, (SELECT count(distinct pu_name) from pre_election_check_info a WHERE inec_arrival_status ='No' and security_arrival_status ='No' and entry_date = (select max(entry_date) from pre_election_check_info b where a.pu_name=b.pu_name)) red_count
FROM pre_election_check_info LIMIT 1";
$sresult = mysqli_query($mysqli, $squery) or die(mysqli_error($mysqli));
$numrows = mysqli_num_rows($sresult);
$total_pu_count = 0;
$total_reported_pu_count = 0;
$count_green_status = 0;
$count_yes_no_status = 0;
$count_no_yes_status = 0;
$count_red_status = 0;
$count_none_status = 0;
if($row = mysqli_fetch_array($sresult)){
$total_pu_count = $row["pu_count"];
$total_reported_pu_count = $row["pu_reported_count"];
$count_green_status = $row["green_count"];
$count_yes_no_status = $row["amber_count_1"];
$count_no_yes_status = $row["amber_count_2"];
$count_red_status = $row["red_count"];
$count_none_status = $total_pu_count - $total_reported_pu_count;
$green_status_percent = $count_green_status/$total_pu_count * 100;
$amber_status_percent = ($count_yes_no_status+$count_no_yes_status)/$total_pu_count * 100;
$red_status_percent = $count_red_status/$total_pu_count * 100;
$none_status_percent = $count_none_status/$total_pu_count * 100;
$red_status_percent+=$none_status_percent;
$return_data .="Green,".$green_status_percent.";";
$return_data .="Amber,".$amber_status_percent.";";
$return_data .="Red,".$red_status_percent.";";
//$return_data .="None,".$none_status_percent.";";
}
return rtrim($return_data,";");
}
显示结果时我得到什么;
绿色,INF;琥珀色;红色,NAN
答案 0 :(得分:1)
除以NAN
,得到0 / 0
,除以INF
,得到0
。
因此,此结果意味着$row['total_pu_count']
,$row['amber_count_1']
,$row['amber_count_2']
和$row['red_count']
均为0
。
进行任何除法之前,应检查$total_pu_count
是否为零。
if ($total_pu_count != 0) {
$green_status_percent = $count_green_status/$total_pu_count * 100;
$amber_status_percent = ($count_yes_no_status+$count_no_yes_status)/$total_pu_count * 100;
$red_status_percent = $count_red_status/$total_pu_count * 100;
$none_status_percent = $count_none_status/$total_pu_count * 100;
$red_status_percent+=$none_status_percent;
}