Jump to content

SELECT table using a variable


speedy33417

Recommended Posts

Is it possible to select a table from a database if I have the name of the table in a variable?

 

Such as

 

$sql = "SELECT settings_id FROM $thisTable WHERE blah blah";

 

Since you are building the SQL statement as a string, you can substitute anything for table. What might be more challenging is to have something like

 

"select setting_id from ? where blah blah"

 

which would be attempting to subsitute the table name as a variable, which I don't think can be done easily

 

 

Can't use ? for table name in prepared statements (or for column names for that matter).

 

Well, you could if you passed it into a stored procedure, built a string, and executed the string...

 

but you are right. I said it wouldn't be easy...  :D

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.