Getting an error trying to select a table name

No you are not - that’s an escaping function that assumes the input is valid. You need to validate the input before passing it to that output function.