Jump to content


Photo

I need to use a variable for the table name in a sql query


  • Please log in to reply
6 replies to this topic

#1 deciblelovechild

deciblelovechild
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 12 February 2003 - 10:09 PM

hi i need some help with this,

i\'ve done some research on this and the only thing i came up with is that dynamic sql is the only way to use a variable for table name. Is this true??? If so, how would you do that with the following code?

$db = $_SESSION[\'User\'];
$db = $db . \"_buddies\";

$online_query = \"select buddy_name from $db WHERE online= \'n\'\";


any help is truly appreciated. thanks in advance....

dlc

#2 metalblend

metalblend
  • Members
  • PipPipPip
  • Advanced Member
  • 89 posts
  • LocationCalifornia, USA

Posted 12 February 2003 - 11:38 PM

That will work.. or you can do it the dirty way:
[php:1:a9971eb821]$online_query = \"select buddy_name from \".$HTTP_SESSION_VARS[\'User\'].\"_buddies WHERE online= \'n\'\";[/php:1:a9971eb821]

Hope that helps.

#3 deciblelovechild

deciblelovechild
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 12 February 2003 - 11:50 PM

i copied and pasted your code and it returns the same error i have been getting all along:

Supplied argument is not a valid MySQL result resource

i read somewhere it is only possible using dynamic sql... do you know if this is true?????

#4 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 13 February 2003 - 01:58 AM

is there any information in the database to gather? ad hoc that query.
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#5 deciblelovechild

deciblelovechild
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 13 February 2003 - 02:03 AM

yea the query works fine. just not when i use a variable as a tablename. have you ever used a variable as a tablename before???

thx
dlc

#6 metalblend

metalblend
  • Members
  • PipPipPip
  • Advanced Member
  • 89 posts
  • LocationCalifornia, USA

Posted 13 February 2003 - 02:57 AM

yes i\'m sure we all have used a variable in a query.. nothing fancy.

see if there\'s an error in the query:[php:1:1527049cfd]$online_query = \"select buddy_name from \".$HTTP_SESSION_VARS[\'User\'].\"_buddies WHERE online= \'n\'\";
mysql_query($online_query) or die(\"Error : query failed.<br>MySQL said: <i>\".mysql_error().\"</i><br>rn\");[/php:1:1527049cfd]

hope that helps.

#7 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 13 February 2003 - 03:07 AM

also echo $online_query out and see if it is was you are expecting.
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users