Jump to content

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


deciblelovechild

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
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.