Jump to content

SESSION to Var


johnrb87

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.