Of course you need dynamic SQL. You can't just say "
select * from tname
" because the interpreter expects that to be an actual table name, not a variable. You'll have to use the working example that you give in your question. Something like
select * from $tname
(if it actually worked) would look messy, be hard to read (how would you know what was being selected), would have security implications (sql injection etc) and would be a nightmare for the parser (lots of hard parses, big CPU overhead), and would also encourage people to use it lots - all of a sudden it's all gone a bit PHP. —
FreshPhilOfSO 33 secs ago