2017-03-08 83 views
0
<?php 
    $con = mysqli_connect('localhost', 'root', '', 'Materniteti'); 
    $f = "select count(*) as f from neonatologji where gjinia=1"; 
    $m = "select count(*) as m from neonatologji where gjinia=2"; 

    $s = $f + $m; 

    $result = mysqli_query($con, $s); 
    $row = mysqli_fetch_assoc($result); 

    echo $row["s"]; 
    $percentage = number_format($f/$s) * 100 . '%'; 

?> 

我想作一個百分比計算,其中的程序選擇所有的女性性別並保存在$f數量和所有在$m而在男性$s應保存的總和爲fm。但該程序不識別$m,所以$s不起作用。從數據庫計數的數據來計算的百分比計算在PHP

我該怎麼辦?

回答

0

給定的代碼有很多問題。

切勿將*與count一起使用,因爲您必須只計算行,那麼單個列就足夠了。因此,替換下面的行:

$f = "select count(*) as f from neonatologji where gjinia=1"; 

$f = "select count(id) as f from neonatologji where gjinia=1"; 

選擇查詢返回結果集。您不能直接使用它,您必須使用fetch_array()將其轉換爲數組。