in reply to how to speed up program?
You appear to be opening a new db connection and reading the same constant array each time through the loop. Try moving that part outside the loop.
After Compline,Zaxo