TRANSFORM status
SELECT orderstatus.ordernum
FROM orderstatus
WHERE orderstatus.ordernum = '$ordernum'
GROUP BY orderstatus.ordernum
PIVOT orderstatus.status
Then pass through the result set and test for (($no_status Is Null) AND (Not $status Is Null)). Since you are filtering by ordernum in WHERE, your result set should always be one entry, I think. Did you intend that?