Fixed bug in mysql function