我正在访问具有500,000行的数据库,每个行在“性别”列中都有一个男性(值1)或女性(值2)条目。
但是,在尝试显示这些数字时,我得到了这样的文字:“共有373名男性和女性”。两个数字都应大于100,000。
我缺少什么明显的语法错误?
$query = "SELECT sex, COUNT(*) FROM data GROUP BY sex";
$result = mysql_query($query) or die(mysql_error());
$sexdb = mysql_fetch_array($result);
$totalmale = $sexdb["1"];
$totalfemale = $sexdb["2"];
echo "<p>There were ".$totalmale." males and ".$totalfemale." females.</p>";
最佳答案
尝试这个..
$query = "SELECT (select count(sex) from data where sex=1) as maleCount,
(select count(sex) from data where sex=2) as femaleCount,
COUNT(*) FROM data GROUP BY sex";
$result = mysql_query($query) or die(mysql_error());
$sexdb = mysql_fetch_assoc($result);
$totalmale = $sexdb["maleCount"];
$totalfemale = $sexdb["femaleCount"];
echo "<p>There were ".$totalmale." males and ".$totalfemale." females.</p>";