I think you are trying to use this line of code inside a function, but forgot to make $db global.

like:
PHP Code:
global $db