php循环正在跳过或遗漏了一些mysql查询的结果

时间:2017-01-07 07:35:23

标签: php mysql sql phpmyadmin sqlyog

php循环跳过或丢失了一些mysql查询的结果,同样的查询在PhpMyAdmin中带来了181个结果,甚至在sqlYOG中,因为循环只带来了175

$queryos = "SELECT * FROM OSCSBranch AS a 
LEFT JOIN
(SELECT ItemCode AS icode FROM NonFood
) AS q ON a.ItemCode = q.icode
WHERE a.BranchId = '$bid' AND a.BType = 'O' AND DATE(a.BDate) = '$date'"; 

$osquery = mysqli_query($conn, $queryos);
if(!$osquery)
{
die("QUERY FAILED OS " . mysqli_error($conn));    
} 

while($row = mysqli_fetch_assoc($osquery)){
$total[$row['ItemCode']] = $row['TotalQuantity'];
}   

1 个答案:

答案 0 :(得分:1)

似乎某些记录具有相同的'ItemCode'

因为$row['ItemCode'] = ....

同一ItemCode的新数据会一次又一次地替换旧值,因为您获得了175条记录而不是181条

所以你可以这样做: -

或者: -

$total[] = $row['TotalQuantity']; will give you like array(0=>array('name'=>'a',....)....)

或者

$total[$row['ItemCode']][] = $row['TotalQuantity']; //will  give you like array('item201'=>array(0=>array(),1=>array(),...),....) (an example)