DondeEstaMiCulo Posted December 12, 2007 Share Posted December 12, 2007 I apologize if this was already address previously, however my search netted 0 results... :-\ I am trying to create list of $_SESSION variables (more like an array I guess) according to my MySQL search results, however they wouldn't carry over into any other pages until I manually specified the names and values. For example: $query = "SELECT * FROM users WHERE userid='123'"; $blah = mysql_fetch_array(mysql_query($query)); foreach($blah as $key => $value) { $_SESSION[$key] = $value; } Pretty simple, or so I thought. But it didn't work. I had to manually specify: $_SESSION['first_name'] = $blah['first_name']; $_SESSION['last_name'] = $blah['last_name']; before it would work. Any ideas on why this is? Thanks in advance. Quote Link to comment Share on other sites More sharing options...
SirChick Posted December 12, 2007 Share Posted December 12, 2007 <?php while($blah = mysql_fetch_array(mysql_query($query)){ $_SESSION['first_name'] = $blah['first_name']; $_SESSION['last_name'] = $blah['last_name']; If($blah as $key => $value){ $_SESSION[$key] = $value; } } ?> not sure if the that if statement will work but the while loop and first 2 sessions should... or have i mis-understood what you were asking for >.< ? Also make sure EVERY PAGE has session_start(); at the TOP of the page... otherwise it won't work.. any page that uses session must have session_start(); before any session processing starts Quote Link to comment Share on other sites More sharing options...
DondeEstaMiCulo Posted December 12, 2007 Author Share Posted December 12, 2007 <?php while($blah = mysql_fetch_array(mysql_query($query)){ $_SESSION['first_name'] = $blah['first_name']; $_SESSION['last_name'] = $blah['last_name']; If($blah as $key => $value){ $_SESSION[$key] = $value; } } ?> not sure if the that if statement will work but the while loop and first 2 sessions should... That's exactly what my problem is... the $_SESSION statements work, but the $_SESSION[$key]=$value loop doesn't... :-\ Any ideas why? Thanks for your help! Brian Quote Link to comment Share on other sites More sharing options...
SirChick Posted December 12, 2007 Share Posted December 12, 2007 what is $key ? and what is $value.. echo them for me... Quote Link to comment Share on other sites More sharing options...
DondeEstaMiCulo Posted December 12, 2007 Author Share Posted December 12, 2007 what is $key ? and what is $value.. echo them for me... $key is whatever the MySQL field name is, and $value is the value contained therein. Example: $query = "SELECT * FROM users WHERE userid='36'"; returns: +--------+------------+-----------+----------------------+ | userid | first_name | last_name | email | +--------+------------+-----------+----------------------+ | 36 | Joe | Mama | joe@example.com | +--------+------------+-----------+----------------------+ The userid, first_name, last_name and email are the $key, and 36, Joe, Mama, joe@example.com are the $value when you use ($blah as $key => $value). The array would look something like this: $blah['userid']=36 $blah['first_name']=Joe $blah['last_name']=Mama $blah['email']=joe@example.com So basically, I just want to copy the keys and values into a new array (basically-speaking) called $_SESSION... But I want the $_SESSION array to be constructed by my MySQL query and not manually. Follow? Thanks, Brian Quote Link to comment Share on other sites More sharing options...
DyslexicDog Posted December 12, 2007 Share Posted December 12, 2007 This is a shot in the dark but give it a try... $_SESSION["$key"] = $value Quote Link to comment Share on other sites More sharing options...
SirChick Posted December 12, 2007 Share Posted December 12, 2007 ($blah as $key => $value) If $key is a field name .... and $value is a number that wouldnt make sense for your checking... because then your effectively asking: if fieldname(a string of chars) is equal to or greater than a number? but how can a word be have a form of value ? Quote Link to comment Share on other sites More sharing options...
revraz Posted December 12, 2007 Share Posted December 12, 2007 <?php $query = "SELECT userid, first_name, last_name, email FROM users WHERE userid = '36'"; $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error()); $count=mysql_num_rows($result); if ($count > 0) { while ($row = mysql_fetch_row($result)) { $_SESSION['uid'] = $row[0]; $_SESSION['fname'] = $row[1]; $_SESSION['lname'] = $row[2]; $_SESSIONl['email'] = $row[3]; } } ?> Quote Link to comment Share on other sites More sharing options...
DondeEstaMiCulo Posted December 12, 2007 Author Share Posted December 12, 2007 This is a shot in the dark but give it a try... $_SESSION["$key"] = $value Yeah, I tried that too but unfortunately it didn't work... Thanks though! ($blah as $key => $value) If $key is a field name .... and $value is a number that wouldnt make sense for your checking... because then your effectively asking: if fieldname(a string of chars) is equal to or greater than a number? but how can a word be have a form of value ? When you use "=>" it sets the variable name as $key and its value as $value. It's not a "equal to or greater than" statement, which would then be specified as >=. <?php $query = "SELECT userid, first_name, last_name, email FROM users WHERE userid = '36'"; $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error()); $count=mysql_num_rows($result); if ($count > 0) { while ($row = mysql_fetch_row($result)) { $_SESSION['uid'] = $row[0]; $_SESSION['fname'] = $row[1]; $_SESSION['lname'] = $row[2]; $_SESSIONl['email'] = $row[3]; } } ?> Thanks for the attempt, however this is what I'm trying to avoid. I don't want to manually specify the $_SESSION names, I want them to be created as a result of my MySQL query. The $_SESSION names should be the same as the field names, and their values should be the same as the field entries... I might try a do...while loop and see if that has any affect. :-\ Thanks for your help anyway, Brian Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted December 12, 2007 Share Posted December 12, 2007 First of all, don't use a do ... while loop. The type of loop is not the problem. Accessing the column/index names in the data from the rows is what the problem is. I have a question (before I work on code to solve this), if there is more than one row from the database, do you want each of the $_SESSION['column_name'] variables to be an array that gets the values from all the rows? Like this - $_SESSION['column_name1'][0] = row 0 value for column name 1 $_SESSION['column_name1'][1] = row 1 value for column name 1 ... $_SESSION['column_name2'][0] = row 0 value for column name 2 $_SESSION['column_name2'][1] = row 1 value for column name 2 ... Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted December 12, 2007 Share Posted December 12, 2007 If we assume that answer to the question about multiple rows is yes, then the following code works (tested on one of my databases) - $query = "SELECT * FROM users WHERE userid='123'"; $result = mysql_query($query) or die("Query failed: " . mysql_error()); while($row = mysql_fetch_assoc($result)) { foreach($row as $key => $value) { $_SESSION[$key][] = $value; } } If this does not work, you will need to define and show what exactly it is or is not doing. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted December 12, 2007 Share Posted December 12, 2007 I created a table on my site to match your example and the following sample code: <?php session_start(); echo '<pre>' . print_r($_SESSION,true) . '</pre>'; include('../dbconfig.php'); mysql_connect($dbhost, $dbuser, $dbpass) or die ("Unable to connect!"); mysql_select_db($dbname); $q = "select * from users where userid = 36"; $rs = mysql_query($q) or die("Problem with the query: $q" . mysql_error()); if (mysql_num_rows($rs) == 1) { $rw = mysql_fetch_assoc($rs); foreach ($rw as $key => $val) $_SESSION[$key] = $val; } ?> The first time you run it, nothing should show up in the $_SESSION array. If you reload the page, the $_SESSION array will be shown. And this script can be run at http://www.rbnsn.com/phpfreaks/db_sessions.php Ken Quote Link to comment Share on other sites More sharing options...
DondeEstaMiCulo Posted December 12, 2007 Author Share Posted December 12, 2007 Hey awesome! You guys nailed it! There will be only one $row returning, since users are unique... So it looks like the only thing I was doing wrong was using mysql_fetch_array instead of mysql_fetch_assoc. How come one fails and the other doesn't? What's the difference between the two? Thanks SO much for your help! Brian Quote Link to comment Share on other sites More sharing options...
DyslexicDog Posted December 13, 2007 Share Posted December 13, 2007 I created a table on my site to match your example and the following sample code: <?php session_start(); include('../dbconfig.php'); mysql_connect($dbhost, $dbuser, $dbpass) or die ("Unable to connect!"); mysql_select_db($dbname); $q = "select * from users where userid = 36"; $rs = mysql_query($q) or die("Problem with the query: $q" . mysql_error()); if (mysql_num_rows($rs) == 1) { $rw = mysql_fetch_assoc($rs); foreach ($rw as $key => $val) $_SESSION[$key] = $val; } echo '<pre>' . print_r($_SESSION,true) . '</pre>'; ?> The first time you run it, nothing should show up in the $_SESSION array. If you reload the page, the $_SESSION array will be shown. And this script can be run at http://www.rbnsn.com/phpfreaks/db_sessions.php Ken Why not move the print_r statement below the session loop? Quote Link to comment 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.