Jump to content

mysql QUERY error


jjdave

Recommended Posts

 

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.

Link to comment
https://forums.phpfreaks.com/topic/2555-mysql-query-error/
Share on other sites

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]

 

Link to comment
https://forums.phpfreaks.com/topic/2555-mysql-query-error/#findComment-8517
Share on other sites

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--]

 

Link to comment
https://forums.phpfreaks.com/topic/2555-mysql-query-error/#findComment-8520
Share on other sites

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.