jjdave Posted September 22, 2005 Share Posted September 22, 2005 Hi All, I have a variable which contains name of a Database Table: How can I pass this variable into the Query to access a data from the table, following are my code. I want to know whats wrong. I hope I am clear enough. I am using mySQL with PHP. $table_name = $_GET['table_name']; $query="SELECT * FROM `$table_name` ORDER BY id"; $result=mysql_query($query); $num=mysql_numrows($result); Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/2555-mysql-query-error/ Share on other sites More sharing options...
Mattyspatty Posted September 22, 2005 Share Posted September 22, 2005 $query="SELECT * FROM `".$table_name."` ORDER BY id ASC|DESC"; Quote Link to comment https://forums.phpfreaks.com/topic/2555-mysql-query-error/#findComment-8481 Share on other sites More sharing options...
AV1611 Posted September 26, 2005 Share Posted September 26, 2005 Are backticks valid for tablename? I thought they were for fieldnames... $table_name = $_GET['table_name']; $query="SELECT * FROM '$table_name' ORDER BY id"; $result=mysql_query($query); $num=mysql_numrows($result); Hi All, I have a variable which contains name of a Database Table: How can I pass this variable into the Query to access a data from the table, following are my code. I want to know whats wrong. I hope I am clear enough. I am using mySQL with PHP. $table_name = $_GET['table_name']; $query="SELECT * FROM `$table_name` ORDER BY id"; $result=mysql_query($query); $num=mysql_numrows($result); Thanks. 298218[/snapback] Quote Link to comment https://forums.phpfreaks.com/topic/2555-mysql-query-error/#findComment-8517 Share on other sites More sharing options...
Cook Posted September 26, 2005 Share Posted September 26, 2005 Backticks are fine for all identifiers within MySQL queries, to protect from using an identifier that would also be a keyword. Your query should be just fine like so, ie the way you put it at first. Is there anything wrong with it? Do you get an error? [!--PHP-Head--][div class=\'phptop\']PHP[/div][div class=\'phpmain\'][!--PHP-EHead--] $table_name = $_GET[\'table_name\']; $query = \"SELECT * FROM `$table_name` ORDER BY `id`\"; [/span][!--PHP-Foot--][/div][!--PHP-EFoot--] Quote Link to comment https://forums.phpfreaks.com/topic/2555-mysql-query-error/#findComment-8520 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.