计算数据库中的数据以计算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";