You are not going to be able to build a variable and then use that variable in the SQL for the tablename just be concatenating it like that. It appears there is a way using prepared statements. Check out the following stack overflow (second answer has an example that may work)
MySQL 1064 Error: You have an error in your SQL syntax check the manual that corresponds to your MySQL server version for the right syntax to use near ‘",pname,’',contents,NULL)) AS ', pname, '')
You have used a single quote that breaks your syntax here in the CONCAT function. Pleasev try this - CONCAT(“max(IF(‘pname’=”,pname,'', contents,NULL).