the SQL statement generated during the loop uses the value $sc_id from the loop definition and bombs out because it is undefined. that's what showed me at first, then i looked at everything else that was going on in there and realized it was undefined the whole time. i placed prints throughout the lop and it seems to be undefined the whole time.