mcfmullen Posted June 20, 2010 Share Posted June 20, 2010 I have a url that goes as follows: http://www.cool.com/page.php?type=Animal&animal=nameAnimal I have a MySQL query as follows: <?php $tablename = $_GET['type']; $animal = $_GET['animal']; $sql = SELECT * from Animal; What I want to do is have the query resemble something like: $sql = SELECT * from $tablename; But I am unable to get the proper syntax. Can anyone help me out? Quote Link to comment https://forums.phpfreaks.com/topic/205332-using-variables-in-place-of-table-names-in-query/ Share on other sites More sharing options...
Alex Posted June 20, 2010 Share Posted June 20, 2010 This is more of a PHP syntax question, so I'm moving it to PHP coding. To answer your question: $sql = "SELECT * from $tablename"; You should really be validating that value though. Quote Link to comment https://forums.phpfreaks.com/topic/205332-using-variables-in-place-of-table-names-in-query/#findComment-1074662 Share on other sites More sharing options...
mcfmullen Posted June 20, 2010 Author Share Posted June 20, 2010 Thank you for doing so. I didn't think this was a php question since it fits inside a MySQL query but I do hope someone can help me! Quote Link to comment https://forums.phpfreaks.com/topic/205332-using-variables-in-place-of-table-names-in-query/#findComment-1074671 Share on other sites More sharing options...
Alex Posted June 20, 2010 Share Posted June 20, 2010 It is a PHP question because it deals with the PHP syntax of constructing the MySQL query and not about the syntax of the query itself. Did you try my solution? Quote Link to comment https://forums.phpfreaks.com/topic/205332-using-variables-in-place-of-table-names-in-query/#findComment-1074677 Share on other sites More sharing options...
mcfmullen Posted June 20, 2010 Author Share Posted June 20, 2010 Yes. I managed to solve the problem by putting a "." in front of the variable. Rest assured, the code is validated! Quote Link to comment https://forums.phpfreaks.com/topic/205332-using-variables-in-place-of-table-names-in-query/#findComment-1074711 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.