In the second case var i has been incremented by the loop so it's ==1 when it tests the second select. It should work if you use var ordersNumRows as the array index. hth