pangi
pangi

Reputation: 2723

How to put data from database to session dynamically

I am pulling data from database and would like to save it into session variables. I would like to name the keys the same that my table cells are named. So for example: I have a cell named "EMAIL", and I would like to get $_SESSION["EMAIL"]

I already have the data from database saved in an array ($data), which has array keys named after the cells, but I would like to move that data to SESSION array, with same keys...

How can I do this dynamically?

Upvotes: 0

Views: 94

Answers (2)

kidonchu
kidonchu

Reputation: 787

Another alternative is

$_SESSION = array_merge($_SESSION, $myArr);

I'm not sure if this is a good practice though.

Upvotes: 1

Ayush
Ayush

Reputation: 42450

You could either do it like this:

foreach($myArr as $k=>$v) {
  $_SESSION[$k] = $v;
}

Or,

$_SESSION['user'] = $myArr;

In the first case, you will access email by doing $_SESSION['EMAIL'], and in the second case, $_SESSION['user']['EMAIL'];

Upvotes: 2

Related Questions