Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
计算数据库中的数据以计算php中的百分比计算_Php - Fatal编程技术网

计算数据库中的数据以计算php中的百分比计算

计算数据库中的数据以计算php中的百分比计算,php,Php,我想做一个百分比计算,程序选择所有女性并将数字保存在$f中,而$m和$s中的所有男性应保存为f和m之和。但是程序无法识别$m,因此$s不起作用 我能做什么?给定的代码有很多问题 永远不要将*与count一起使用,因为您必须只对行进行计数,这样单个列就足够了。因此,请替换以下行: <?php $con = mysqli_connect('localhost', 'root', '', 'Materniteti'); $f = "select count(*) as f fro

我想做一个百分比计算,程序选择所有女性并将数字保存在
$f
中,而
$m
$s
中的所有男性应保存为
f
m
之和。但是程序无法识别
$m
,因此
$s
不起作用


我能做什么?

给定的代码有很多问题

永远不要将*与count一起使用,因为您必须只对行进行计数,这样单个列就足够了。因此,请替换以下行:

<?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 . '%';

?>

选择查询返回结果集。您不能直接使用它,必须使用
fetch\u array()
将其转换为数组

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