johnrb87 Posted August 3, 2010 Share Posted August 3, 2010 Hello everyone I have the following code $num = 1; $query = mysql_query("SELECT * FROM people"); while($row = mysql_fetch_array($query)) { $nums = $num++; $_SESSION['equal'.$nums.''] = $row['name']; $_SESSION['total'.$nums.''] = $row['age']; } which basically returns me SESSION names by increasing by 1, so it could produce the following session names equal1 total1 equal2 total2 equal3 total3 equal4 total4 instead of setting those as sessions, I simply want to set them as vars, so instead of $_SESSION['equal'.$nums.''] = $row['name']; $_SESSION['total'.$nums.''] = $row['age']; it would look like $equal.$nums = $row['name']; $total.$nums = $row['age']; I have tried $equal.$nums = $row['name']; $total.$nums = $row['age']; but it doesn't seem to work any ideas? thanks Link to comment https://forums.phpfreaks.com/topic/209654-session-to-var/ Share on other sites More sharing options...
JonnoTheDev Posted August 3, 2010 Share Posted August 3, 2010 These are known as variable variables. http://php.net/manual/en/language.variables.variable.php Link to comment https://forums.phpfreaks.com/topic/209654-session-to-var/#findComment-1094524 Share on other sites More sharing options...
johnrb87 Posted August 3, 2010 Author Share Posted August 3, 2010 Thanks I'm quite new to PHP, the page was quite overwhelming, but I tried $equal.$nums = $row['name']; $total.$nums = $row['age']; but that didn't seem to work, and also tried $equal[$nums] = $row['name']; $total[$nums] = $row['age']; but that also failed. Any suggestions on what I can try? Thanks Link to comment https://forums.phpfreaks.com/topic/209654-session-to-var/#findComment-1094542 Share on other sites More sharing options...
JonnoTheDev Posted August 3, 2010 Share Posted August 3, 2010 Your second option to use an array is the best option as opposed to variable variables. <?php $results = array(); $num = 1; $query = mysql_query("SELECT * FROM people"); while($row = mysql_fetch_array($query)) { $results[$num] = array('name' => $row['name'], 'age' => $row['age']); $num++; } /* now you can extract values from the array */ print "name: ".$results[1]['name'].", age: ".$results[1]['age']."<br />"; print "name: ".$results[2]['name'].", age: ".$results[2]['age']."<br />"; ?> Link to comment https://forums.phpfreaks.com/topic/209654-session-to-var/#findComment-1094544 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.