First off, it's hard to read your code since you don't indent it properly. You also lack a closing }, but I assume it's supposed to come after the last echo.
Also, since you claim that your code isn't working like it should, I'm going to have to assume that $QTY is supposed to be the total for the query inside the outer loop only, and not a grand total. If this assumption is correct, then
while (outer loop) {
// ...
$QTY = 0; // Else it will be a running total.
while ($row2 = db2_fetch_assoc($stmt2)){
$OnHand = $row2['IQTYOH'];
$QTY += $OnHand;
}
// ...
}
But, if you havn't removed code to get a minimum working example, there is no need to select the columns that you don't use, and you could instead go for an inner query of
$sqlQTY = "SELECT sum(IQTYOH) FROM files.itemmast
WHERE IITEM = '".$row['IITEM']."'";