Rzj Hayabusa
Rzj Hayabusa

Reputation: 657

How to combine the $_SESSION into one variable in PHP mysqli ?

normally, i use the single $_SESSION for single field in the table as below in mysqli, let's say i use 'email' field in users table

$email              = mysqli_real_escape_string($connect,$_POST["email"]);
$_SESSION['email']  =$email;

but it only can use email as sessions right ?

But what if i have multiple fields in the users table such as username, password, gender ?. How to combine/bind all the $_SESSION fields together into one variable ?

$user = $sessions_variable['username']; 

thanks in advance :)

Upvotes: 0

Views: 305

Answers (3)

Bhavin
Bhavin

Reputation: 2158

We can create multidimensional array for session follow the below procedure. Example:

$_SESSION['foo']['foo_1']  =$foo1;
$_SESSION['foo']['foo_2']  =$foo2;
$_SESSION['foo']['foo_3']  =$foo3;
...

Now if we want to use whole foo array in single variable. you have to pass it like this.

$foo_whole_session = $_SESSION['foo']; 

You can check what you found using print_r($_SESSION).

Upvotes: 0

Your Common Sense
Your Common Sense

Reputation: 157889

$_SESSON is an array. So your first move is to read up on arrays in the PHP manual.

After that you'll be able to either assign different values to different $_SESSON elements

$_SESSION['email'] = $email;
$_SESSION['username'] = $username;

or, after getting the user info from a database, assign it to a single element

$_SESSION['user'] = $row;

Upvotes: 2

shubham715
shubham715

Reputation: 3302

You can create multidimensional array in session like this

$_SESSION['user']['username']  =$username;
$_SESSION['user']['email']  =$email;

then you can pass whole user session array to single variable.

$user = $_SESSION['user']; 

Upvotes: 0

Related Questions