Is it possible to send 2 one-line arrays with fetch_array to the $_session?
if(mysqli_num_rows($result) > 0){
$row = mysqli_fetch_array($result);
if($row['user_type'] == 'admin'){
$_SESSION['admin_name'] = $row['name'];
header('location:admin_page.php');
I want to send $row['name'] and $row['email']
to print through echo on the page.
<php echo ($_SESSION['admin_name']['name']); echo '<br>'; echo ($_SESSION['admin_name']['email']); ?>
some material on the subject
>Solution :
Your question is a bit muddled, and suggests a few things you haven’t fully understood.
- $_SESSION isn’t something you "send to" – it’s not a function, or a service. It’s something you store things in; and more specifically, it’s an array you can assign things as items in.
- $row[‘name’] and $row[’email’] in your example are not "one-line arrays", they’re single values, presumably strings. They happen to be stored inside an array, but that’s irrelevant to what you do with them individually.
- $row is an array. If you want to, you can pass around and store that whole array, including everything inside it. In PHP an array can contain any kind of item, including other arrays.
With all that in mind, there’s really nothing different about storing two things in $_SESSION compared to storing one thing – just assign them both with whatever key names you want:
$_SESSION['full_name'] = $row['name']
$_SESSION['email_address'] = $row['email'];
Or if you want to, you can store the whole array from the database in one key, and access the values inside it later:
$_SESSION['user'] = $row;
// ...
echo $_SESSION['user']['name'];
Or create a new array and store that:
$user = [
'full_name' => $row['first_name'] . ' ' . $row['last_name'],
'email' => $row['email'],
];
$_SESSION['user'] = $user;
The power of programming is that you can recombine the pieces into endless combinations, once you understand properly what each piece is.