简体   繁体   中英

How do I store multiple variables in a single session?

This is what I have, what do I do? I want to store two variables in a single session, they are username and avatar.

if ($count == 1) 
{
$row = mysqli_fetch_array($result);
//while ($_SESSION['username'] = $row['username'])
$_SESSION['users'] = [''];
$_SESSION['users']['username'] = $row['username'];
$_SESSION['users']['avatar'] = $row['avatar'];

{
session_start();

Is there a reason you can't use:

$_SESSION['username'] = $row['username'];
$_SESSION['avatar'] = $row['avatar'];

First: Use session_start(); of the top of your code.

Second: What you mean with this row? "$_SESSION['users'] = [''];" If you want to initialize it you should use:

$_SESSION['users'] = array();

In short your code should like:

session_start();
.
.
.
if ($count == 1) 
{
$row = mysqli_fetch_array($result);
$_SESSION['users'] = array();
$_SESSION['users']['username'] = $row['username'];
$_SESSION['users']['avatar'] = $row['avatar'];
...

I hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM