Table name as session variable.

  • digisales
  • Newbie
  • Newbie
  • digisales
  • Posts: 9

Post 3+ Months Ago

I have set a mysql table name as one of my SESSION variables. I have no problem calling a SESS VAR within html using
Code: [ Select ]
<? echo $_SESSION['variable']; ?>


But how do I get it into the following?

Code: [ Select ]
<?
$db_name = "dbase";
$table_name = "tname";
$connection = @mysql_connect("localhost", "x_user", "passw")
        or die(mysql_error());
$db = @mysql_select_db($db_name, $connection) or die(mysql_error());
 
//start creating the SQL statement
$sql = "INSERT INTO $table_name
(t, f_name, l_name, email_addr,date) VALUES ('', '$_POST[f_name]','$_POST[l_name]','$_POST[email_addr]',now())";
 
$result = @mysql_query($sql, $connection) or die (mysql_error());
 
?>
  1. <?
  2. $db_name = "dbase";
  3. $table_name = "tname";
  4. $connection = @mysql_connect("localhost", "x_user", "passw")
  5.         or die(mysql_error());
  6. $db = @mysql_select_db($db_name, $connection) or die(mysql_error());
  7.  
  8. //start creating the SQL statement
  9. $sql = "INSERT INTO $table_name
  10. (t, f_name, l_name, email_addr,date) VALUES ('', '$_POST[f_name]','$_POST[l_name]','$_POST[email_addr]',now())";
  11.  
  12. $result = @mysql_query($sql, $connection) or die (mysql_error());
  13.  
  14. ?>

I have tried;
Code: [ Select ]
$table_name = "$_SESSION['tname']";

but the syntax is apparently wrong.

Any help is much appreciated.
Code: [ Select ]
 
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

Try dropping the quotes surrounding the session var. Also look to make sure that you're setting it into your session as 'tname' when you save it. I know those are trivial suggestions, but your syntax should work if the variable is set and present.

Code: [ Select ]
$tbl = $_SESSION['tname'];
echo "My table name is $tbl";
  1. $tbl = $_SESSION['tname'];
  2. echo "My table name is $tbl";
  • digisales
  • Newbie
  • Newbie
  • digisales
  • Posts: 9

Post 3+ Months Ago

That did it!
I thought I had already tried that late last night, but obviously not. Thanks so much.
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

No problem, if you get stuck again, you know where to find us. :)
  • casablanca
  • Proficient
  • Proficient
  • User avatar
  • Posts: 481

Post 3+ Months Ago

If you'd like it to work with quotes, you need to surround the variable with braces, like this:
Code: [ Select ]
$table_name = "{$_SESSION['tname']}";
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

Yes, well that's a bit overkill for a simple single-variable assignment, but the theory is worth noting in case you would like to include such a variable in the middle of a string or something down the road.

Just because something is possible doesn't make it right for every situation. Avoiding things like that helps to prevent unnecessary code obfuscation. Simplicity always lends a helping hand to maintainability.
  • digisales
  • Newbie
  • Newbie
  • digisales
  • Posts: 9

Post 3+ Months Ago

Actually that additional post was helpful, since I do have a "string" situation that I want to try brackets with! Thanks to both of you very much.
  • casablanca
  • Proficient
  • Proficient
  • User avatar
  • Posts: 481

Post 3+ Months Ago

UPSGuy wrote:
Just because something is possible doesn't make it right for every situation. Avoiding things like that helps to prevent unnecessary code obfuscation. Simplicity always lends a helping hand to maintainability.

No doubt about that, I follow that myself. I just posted an alternate solution since digisales was trying to use quotes.

Post Information

  • Total Posts in this topic: 8 posts
  • Users browsing this forum: No registered users and 154 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.