php loop is skipping or missing some results of mysql query

149 views Asked by At

php loop is skipping or missing some results of mysql query, the same query is bringing 181 results in PhpMyAdmin and even in sqlYOG where as the loop is bringing only 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

There are 1 answers

5
Death-is-the-real-truth On BEST ANSWER

It seems like some records have same 'ItemCode'.

So due to $row['ItemCode'] = ....

the new data of same ItemCode is replacing the older value again and again and due to that you are getting 175 record instead of 181

So you can do it like below:-

Either:-

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

Or

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