Kingskin Posted July 19, 2006 Share Posted July 19, 2006 Ok hopefully somone can help me with this as its driving me crazy!I'm trying toi register some session variables based on db fields within a loop when a user logs in. Here is the code im using:[code]<?function sqlquery($sql){ $result = mysql_query($sql); $row = mysql_fetch_array($result); if(!$result){ echo '<p>SQL Error: '.$sql.'</p>'; } return $row;}function reg_session_vars($userid){ $row = sqlquery("SELECT * FROM users WHERE userid = $userid"); foreach($row as $key => $value){ $_SESSION[$key] = $value; } session_write_close();}[/code]This seems to register the variables fine until I navigate to another page, when they are emptied. If i print_r on the login page itself, I get my $_SESSION array nicely filled, but as soon as I navigate away it's gone.If i register sessions like this:[code]$_SESSION['userid'] = $userid;$_SESSION['username'] = $username;$_SESSION['first_name'] = $first_name;$_SESSION['last_name'] = $last_name;etc...[/code]It works fine. Why is it my loop isn't working properly, and can anyone help me populate $_SESSION from a db table with a loop please? I'd rather it was dynamic than be having to add & remove lines if the table structure changes. Quote Link to comment https://forums.phpfreaks.com/topic/15057-session-vars-disappear-when-loop-is-used-to-assign-them/ Share on other sites More sharing options...
wildteen88 Posted July 19, 2006 Share Posted July 19, 2006 Code looks fine to me, and I cant see nout wrong with it. However i'd suggest you use mysql_fetch_assoc rather than mysq_fetch_array, otherwise you'll get duplicate sessions, one with a numerical key and another with a string key eg: for your userid session, you'll get userid sessions like so:$_SESSION[0] and $_SESSION['userid']With mysql_fetch_assoc this wont happen.Another suggestion which may not help but chnage this:[code]$_SESSION[$key] = $value;[/code]to:[code]$_SESSION[{$key}] = $value;[/code]Also make sure you have session_start(); as the first line for everypage that use's sessions. Quote Link to comment https://forums.phpfreaks.com/topic/15057-session-vars-disappear-when-loop-is-used-to-assign-them/#findComment-60573 Share on other sites More sharing options...
Kingskin Posted July 19, 2006 Author Share Posted July 19, 2006 [quote author=wildteen88 link=topic=101127.msg399930#msg399930 date=1153331611]Code looks fine to me, and I cant see nout wrong with it. However i'd suggest you use mysql_fetch_assoc rather than mysq_fetch_array, otherwise you'll get duplicate sessions, one with a numerical key and another with a string key eg: for your userid session, you'll get userid sessions like so:$_SESSION[0] and $_SESSION['userid']With mysql_fetch_assoc this wont happen.Another suggestion which may not help but chnage this:[code]$_SESSION[$key] = $value;[/code]to:[code]$_SESSION[{$key}] = $value;[/code]Also make sure you have session_start(); as the first line for everypage that use's sessions.[/quote]cheers dude I had notices that duplicate session data which you mentioned, was going to be my next question! :DThe suggestion regarding the curly braces actually resulted in an error, however it seems that changing to mysql_fetch_assoc seems to have cleared the problem anyway! I remember reading that session keys can't be numbers, so this must be the cause.Thanks for the help, problem solved :) Quote Link to comment https://forums.phpfreaks.com/topic/15057-session-vars-disappear-when-loop-is-used-to-assign-them/#findComment-60575 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.