It's better to minimize the use of globals. May be you can pass db and table as parameters to the function