Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.