in reply to MYSQL while function

Apart from what others have mentioned, consider removing those backticks in the first line of the SQL query. No reason to quote the table name -- it may not even be legal. And if it were legal, backticks would probably not be the right way to quote.

blokhead