Methok Posted February 17, 2008 Share Posted February 17, 2008 Hi, Is it possible to get a variable for a table name to work in a SELECT query? $query = "SELECT * FROM $table"; Tried several ways but cant get it to use $tabel as my table name. I can echo $table and it will give me the corerct name, just not able to get it to work in the query. As you have probably guessed im new to any kind of programming or what little I do know is not worth talking about (lsl anyone? ) Any help would be great, Thank you Methok Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted February 17, 2008 Share Posted February 17, 2008 Yes, you can. Here is an example: <?php $table = "table_name"; $query = mysql_query("SELECT * FROM $table")or die(mysql_error()); ?> Make sure you have that or die(mysql_error()) at the end of your query. Quote Link to comment Share on other sites More sharing options...
Methok Posted February 18, 2008 Author Share Posted February 18, 2008 Ecellent. This helped. Thank you. Methok Quote Link to comment Share on other sites More sharing options...
aschk Posted February 18, 2008 Share Posted February 18, 2008 Indeed there shouldn't be a problem with variable substitution into a MySQL query. It is nothing more than a string anyway. Thus as poco suggested you should see your error if that table_name doesn't exist , which I suspect is what might have been causing your error in the first place. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.