Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jan 2007
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How do I convert session variables to local variables?

    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:

    PHP Code:
    $los=$_SESSION['answer'][los]; 
    $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.

    PHP Code:
    $age={$_SESSION['answer'][age]}; 
    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

    PHP Code:
    $query="INSERT INTO HGsurvey 
                VALUES ('$_SESSION['answer'][age]','$_SESSION['answer'][los]','$_SESSION['answer'][eecat]')"
    ;
            
    mysql_query($query
            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:

    PHP Code:
    $query="INSERT INTO HGsurvey 
                VALUES ('$age',$los','eecat')"
    ;
            
    mysql_query($query
            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!

  • #2
    Senior Coder koyama's Avatar
    Join Date
    Dec 2006
    Location
    Copenhagen, Denmark
    Posts
    1,246
    Thanks
    1
    Thanked 5 Times in 5 Posts
    If $los is empty after this statement (syntax is ok)
    PHP Code:
    $los=$_SESSION['answer'][los]; 
    then the query part will not work in any case. So you should resolve this first. Try to do this
    PHP Code:
    var_dump($_SESSION); 
    at the beginning of your script to get the full view of your session variables. I recommend that you for now write like this:
    PHP Code:
    $los $_SESSION['answer']['los'];  // instead of $_SESSION['answer'][los] 
    or when used in expressions with double quotes use braces like this:
    PHP Code:
    $los "{$_SESSION['answer']['los']}"
    Both should work.

  • #3
    New Coder
    Join Date
    Jan 2007
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    koyoma,

    Thank you for your reply!

    Will try that out and let you know how it goes


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •