How to use prepared statements within a function