下面的代码在屏幕上显示nan和inf值

y53ybaqx  于 2021-06-19  发布在  Mysql
关注(0)|答案(1)|浏览(303)

下面的代码在显示mysql中的值时在屏幕上显示inf和nan值。我在mysql中运行了这个查询,得到了所需的值,但是在返回值的同时,我在web页面上得到了上面的值。

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;琥珀,楠;红色,南

ctzwtxfj

ctzwtxfj1#

你得到了吗 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;
}

相关问题