View Full Version : How do I convert session variables to local variables?

01-08-2007, 11:41 AM
I'm sure this is a very simple solution. I have found a few threads with similar problems but I have tried the solutions (namely checking that the session_start() was on every page and putting the { in front of the session variable, but both don't seem to be working for me.

As such, how do I convert a session variable to a local variable?

I've currently got this code:


$los remains empty though.

When I tried the following, I got a parse error saying that there was an unexpected '{' on the line where i tried to convert it.


Can someone please tell me how to do this?

I've stored my session variables and have checked, and they work perfectly.

The problem I'm having is in referring to my session variables in my SQL statement

$query="INSERT INTO HGsurvey
VALUES ('$_SESSION['answer'][age]','$_SESSION['answer'][los]','$_SESSION['answer'][eecat]')";
or DIE ("Couldn't insert questionnaire");

I kept getting a parse error that it was expecting a T_Variable and something about whitespace.

As such, I'm trying to assign the session variable to a local variable so that I could do this instead:

$query="INSERT INTO HGsurvey
VALUES ('$age',$los','eecat')";
or DIE ("Couldn't insert questionnaire");

I tested it using echo, but while the session variable returned the value submitted in the form, the local variable returned an empty value.

As a note, I have ensured that every page has session_start(); at the top.

Thanks again!

01-08-2007, 01:15 PM
If $los is empty after this statement (syntax is ok)


then the query part will not work in any case. So you should resolve this first. Try to do this


at the beginning of your script to get the full view of your session variables. I recommend that you for now write like this:

$los = $_SESSION['answer']['los']; // instead of $_SESSION['answer'][los]

or when used in expressions with double quotes use braces like this:

$los = "{$_SESSION['answer']['los']}";

Both should work.

01-08-2007, 01:40 PM

Thank you for your reply!

Will try that out and let you know how it goes :thumbsup: