2017-03-08 136 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()将其转换为数组。