我正在从 mysql 中提取一些数据并计算匹配的出现次数(似乎等同于WHERE foo='bar'
)。但是,当我循环遍历 PHP 中的数据时,我的计数远低于 mysql 中的计数。
MYSQL> SELECT COUNT(foo) FROM database.table WHERE foo='bar';
# PHP
while ($response = mysql_fetch_assoc($surveydata)){
if ($response==='bar') {
$bar++;
}
}
数据可能包含一个或多个&
,所以我只想匹配bar
而不匹配bar & foobar
。我怀疑 mysql 正在计数bar
,bar & foobar
而 php 只是计数bar
而不是bar & foobar
. php在返回1210
,mysql在返回1783
,所以手动统计看看谁是对的,确实很实用……</p>
我四处搜索,但很惊讶“mysql完全匹配”或“mysql完全相等”x,x没有相关
编辑
这是实际的sql
SELECT COUNT(`race`) FROM `database`.`table` WHERE `completion status`='complete';
和实际的php
mysql_query("SELECT `race`,`etcetera` FROM `database`.`table` WHERE `completion status`='complete';");
$demographics=array(
"race"=>array(
"White"=>array('consented'=>0,'partial'=>0,'completed'=>0),
"Black"=>array('consented'=>0,'partial'=>0,'completed'=>0),
"Hispanic"=>array('consented'=>0,'partial'=>0,'completed'=>0),
"Asian"=>array('consented'=>0,'partial'=>0,'completed'=>0),
"Pacific Islander"=>array('consented'=>0,'partial'=>0,'completed'=>0),
"Multiracial"=>array('consented'=>0,'partial'=>0,'completed'=>0),
"Other"=>array('consented'=>0,'partial'=>0,'completed'=>0)
)
//more
);
while ($dbrecord = mysql_fetch_assoc($surveydata)) {
foreach ( $dbrecord as $dbfield=>$dbcellval ) {
foreach ( $demographics as $demographic=>&$options ) {
foreach ( $options as $option=>&$counter ) {
if ( $option==="Multiracial" && strpos($dbcellval,'&') >0 && strpos($dbcellval,'&')!==false ) {
if ($dbrecord['consent']==="1"){
$demographics["race"]["Multiracial"]['consented']++;
if ($dbrecord['completion status']==="partial") {
$demographics["race"]["Multiracial"]['partial']++;
} // if
else if ($dbrecord['completion status']==="complete") {
$demographics["race"]["Multiracial"]['completed']++;
} // else if
} // if
}
else if ($option===$dbcellval){
if ($dbrecord['consent']==="1"){
$counter['consented']++;
if ($dbrecord['completion status']==="partial") {
$counter['partial']++;
} // if
else if ($dbrecord['completion status']==="complete") {
$counter['completed']++;
} // else if
} // if
} // else if $option==$item
} // foreach $options
} // foreach $demographics
} // foreach $dbrecord
} // while
来自的数据SELECT race FROM database.table
如下:
White & Black
White
White & Asian
White & Asian & Black
Asian
Asian & Black
// etc