Set array as session

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TheServant
    Recognized Expert Top Contributor
    • Feb 2008
    • 1168

    Set array as session

    I am trying to get data from a MySQL table and then store it in the session. I used to define each variable like:
    Code:
    $_SESSION['var1'] = $table['var1'];
    $_SESSION['var2'] = $table['var2'];
    But after re-doing my code, there has to be an easier way. First I simply tried:
    Code:
    $_SESSION = $table
    Then
    Code:
    foreach( $user_data as $key => $value ) {
    		$_SESSION[$key] = $value;
    	}
    Probably really stupid mistake I'm making, but can someone help me out?
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    what about
    Code:
    $_SESSION['db_data'] = $table;
    ?

    Comment

    • TheServant
      Recognized Expert Top Contributor
      • Feb 2008
      • 1168

      #3
      But then the array will be stored as a variable and I will have to call it via:
      Code:
      $_SESSION['db_data']['var1']
      Won't I? I will try it when I get home, but there's got to be a logical way to transfer keys and values to another array.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        sure, but what's the problem with it?

        Comment

        • TheServant
          Recognized Expert Top Contributor
          • Feb 2008
          • 1168

          #5
          lol, I'm lazy and looking for less coding. I think what I'm going to do anyway is simply have one variable in my session and the rest I will call from MySQL as needed per page. This means two MySQL queries per page, but for the sake of 10 variables it might be quicker than a 100 variable array plus one MySQL query? Not sure...

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            probably. and if you use prepared statements, it could be even quicker to query the DB twice (despite the memory advantage), but that's only a guess.

            Comment

            • TheServant
              Recognized Expert Top Contributor
              • Feb 2008
              • 1168

              #7
              What do you mean prepared statements?

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                see Parameterized Statements – Wikipedia and Prepared Statements – MySQL.

                Comment

                • Markus
                  Recognized Expert Expert
                  • Jun 2007
                  • 6092

                  #9
                  So, parametrized statements abstract the data out of the statement, and I assume it is then cleansed through the binding?

                  I have used this method before (still do, in fact) but I wasn't aware of it's name or it's benefits.

                  - Mark.

                  Comment

                  • TheServant
                    Recognized Expert Top Contributor
                    • Feb 2008
                    • 1168

                    #10
                    Interesting. Thanks for the info.

                    Comment

                    Working...