Jump to content

Archived

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

jjdave

mysql QUERY error

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.

Share this post


Link to post
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]

 

Share this post


Link to post
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--]

 

Share this post


Link to post
Share on other sites

×

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.