Jump to content


Photo

mysql QUERY error


  • Please log in to reply
3 replies to this topic

#1 jjdave

jjdave
  • Members
  • Pip
  • Newbie
  • 6 posts

Posted 22 September 2005 - 07:22 PM


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.

#2 Mattyspatty

Mattyspatty
  • Members
  • PipPipPip
  • Advanced Member
  • 76 posts
  • LocationEngland :D

Posted 22 September 2005 - 07:41 PM

$query="SELECT * FROM `".$table_name."` ORDER BY id ASC|DESC";
ok, so my PHP is a bit rusty... my replies are 90% ok.
the other 10% either have syntax errors or are the "hard solution"
http://www.php.net  http://www.google.com - Are your friends!

I have a C++ problem with 'keybd_event' in certain applications. if you can help me PM me! :P
Needs motivation to create another quick cash website. I need a new TV

#3 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 26 September 2005 - 01:17 AM

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.

View Post



#4 Cook

Cook
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts
  • LocationSingapore

Posted 26 September 2005 - 02:01 AM

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

Cook




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users