Hi all!

I'm sure this is a very simple solution, but I haven't been able to find in in my search through Yahoo, Google and a few forums.

How do I convert a session variable to a local variable?

I've currently got this code:

$los=$_SESSION['answer'][los];

$los remains empty though.

Can someone please tell me how to do this?

I'll greatly appreciate it!

Thanks a bunch in advance!

    Hi there,
    Make sure that you store your required variables in the session.

    can you post the code in which you are storing your session data so that we can help you more

      OOP,

      Thank you for your quick reply!

      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]')";
      		mysql_query($query) 
      		or DIE ("Couldn't insert questionnaire");

      I kept getting a parse error that it was expecting a T_Variable.

      As such, I tried assigning the session variable to a local variable so that I could do this instead:

      $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.

      $age=$_SESSION['answer'][age]

      As such, I'd like to know how i can assign the value of the session variable to a local variable.

      Thanks again!

        query="INSERT INTO HGsurvey
                    VALUES ('{$_SESSION['answer']['age']}','{$_SESSION['answer']['los']}','{$_SESSION['answer']['eecat']}')";
        

        This should work in your query. Without {} it tries to evaluate $_SESSION['answer'] alone, which is an array.

          Wilku!

          WOOHOO!!! It works! 😃

          Thanks a lot! Managed to store that in the database! You really made my day!

          But assuming that I'd like to store it as a local variable, does that mean that i'll do it like this?

          $age={$_SESSION['answer']['age']}
            $age =  $_SESSION['answer']['age'];
            

            This should be enough.

              Wilku,

              Thanks again for your prompt reply!

              I've tried that, and $age remains null even though $_SESSION['answer'][age] is not.

              Any ideas?

                Shrrie you are not pating attention to the answers. You need to put quotes around age as well as answer.

                You keep writing $SESSION['answer'][age] when it should be $SESSION['answer']['age']

                  Roger Ramjet,

                  Thank you for your post, it made me check my code and I found that I had done the following when I posted the values to the session variables:

                  function next_page()
                  {
                  $_SESSION['answer'][age] = $_POST['age']; 
                  $_SESSION['answer'][los] = $_POST['los']; 
                  $_SESSION['answer'][eecat] = $_POST['eecat']; 
                  $_SESSION['answer'][division] = $_POST['division']; 
                  $_SESSION['answer'][dept] = $_POST['dept'];
                  $_SESSION['answer'][section] = $_POST['section']; 
                  header("Location:page1.php");
                  }

                  which i then amended to:

                  function next_page()
                  {
                  $_SESSION['answer']['age'] = $_POST['age']; 
                  $_SESSION['answer']['los'] = $_POST['los']; 
                  $_SESSION['answer']['eecat'] = $_POST['eecat']; 
                  $_SESSION['answer']['division'] = $_POST['division']; 
                  $_SESSION['answer']['dept'] = $_POST['dept'];
                  $_SESSION['answer']['section'] = $_POST['section']; 
                  header("Location:page1.php");
                  }

                  also amended all my code. Thanks for pointing that out!

                  stolzboy,

                  Thanks for your reply!

                  Yes, I've tried that and I got a parse error saying that there was an unexpected '{'

                  So it still doesn't work.

                  Here's my code to assign the variables:

                  function var_value()
                  {
                  $age=$_SESSION['answer']['age'];
                  $los=$_SESSION['answer']['los'];
                  $eecat=$_SESSION['answer']['eecat'];
                  $division=$_SESSION['answer']['division'];
                  $dept=$_SESSION['answer']['dept'];
                  $section=$_SESSION['answer']['section'];
                  }

                  And this is how I checked to see if they worked:

                  function display_var()
                  {
                  echo $_SESSION['answer']['age'];
                  echo "<br>";
                  echo $age;
                  echo "<br>";
                  echo $_SESSION['answer']['los'];
                  echo "<br>";
                  echo $los;
                  echo "<br>";
                  echo $_SESSION['answer']['eecat'];
                  echo "<br>";
                  echo $eecat;
                  echo "<br>";
                  echo $_SESSION['answer']['division'];
                  echo "<br>";
                  echo $division;
                  echo "<br>";
                  echo $_SESSION['answer']['dept'];
                  }
                  
                  function submit_form()
                  {
                  var_value();
                  display_var();
                  }

                  The value of the session variable is displayed while the other is just a blank line.

                  Thanks again for all your help!

                    Have a look at your register globals setting.

                    By which I mean that if it is off then the vars are only avaialable within the scope of the function that defines them, so your display function is creating new empty vars when you display them.

                      $age and others are local varibles in var_value() so they are not seen in display_var(). Try putting your code in one function.

                      function submit_form()
                      {
                      $age=$_SESSION['answer']['age'];
                      $los=$_SESSION['answer']['los'];
                      $eecat=$_SESSION['answer']['eecat'];
                      $division=$_SESSION['answer']['division'];
                      $dept=$_SESSION['answer']['dept'];
                      $section=$_SESSION['answer']['section']; 
                      echo $_SESSION['answer']['age'];
                      echo "<br>";
                      echo $age;
                      echo "<br>";
                      echo $_SESSION['answer']['los'];
                      echo "<br>";
                      echo $los;
                      echo "<br>";
                      echo $_SESSION['answer']['eecat'];
                      echo "<br>";
                      echo $eecat;
                      echo "<br>";
                      echo $_SESSION['answer']['division'];
                      echo "<br>";
                      echo $division;
                      echo "<br>";
                      echo $_SESSION['answer']['dept']; 
                      } 
                      

                      It really should work 🙂

                        Do you execute a session_start() on each applicable page prior to any references the $_SESSION array?

                          Roger Ramjet wrote:

                          yes

                          heh, i think he's asking her if she did, not if you are suppose to 😉

                            Whoops, missed that - thought it was Sherrie posting, me bad

                              Thanks for all your replies! 🙂

                              Yes, each and every page starts the session.

                              Hmmm... I can see where I've gone wrong now. I was under the assumption that the variables would be available for the entire file... How would I turn my global settings on?

                                you'd be better served to leave register_globals off and rethink/redo the way you are handling your variables

                                  stolzyboy wrote:

                                  you'd be better served to leave register_globals off and rethink/redo the way you are handling your variables

                                  Here here. Don't ever turn them on. Learn about variable scope, it is an important issue in every programming language. If you want to make a var available across your whole application then use $_SESSION, that is what it is for.

                                    stolzboy & Roger Ramjet,

                                    Thanks a lot for your replies!

                                    K, will not turn them on then, and have used the session variables in mySQL query... seems a lil long typing all the session variables, but it works perfectly!

                                    Thanks a lot!