dongxu0690 2019-05-02 20:59
浏览 76
已采纳

如何从MYSQL数据库分配的PHP会话数组中打印单个变量

PHP session array created from database and want to print to screen only one variable. I'm using this guide.

The array is created by :

$user = $libUsr->check($_POST['email'], $_POST['password']);
if ($user===false) { echo "ERR"; }
else {
$_SESSION['user'] = [
"name" => $user['user_name'],
"email" => $user['user_email'],
"status" => $user['user_status'],
"data" => $user['user_data']
];
echo "OK";
}
break;

The header of the signedin page after login is below, with start_session() called in 2a-config.php

require __DIR__ . DIRECTORY_SEPARATOR . "lib" . DIRECTORY_SEPARATOR . "2a- 
config.php";
if (!is_array($_SESSION['user'])) {
header("Location: index.php");
die();
}

I've tried all combinations such as these (with the php tags of course):

echo $user["name"];
echo $user[1]["name"];
print_r(array_column($user, ‘name’));

Was even getting desperate as I've spent a lot of time on this and no other answers I found was working so was even trying

echo $_SESSION["name"];
echo $_SESSION[1]["name"];

As well as these previously asked questions but still can't get it to work and no errors are coming up in my error log.

This is the array when I var dump.

ARRAY(1) { ["USER"]=> ARRAY(4) { ["NAME"]=> STRING(4) "TEST" ["EMAIL"]=> 
STRING(13) "TEST@TEST.COM" ["STATUS"]=> STRING(1) "A" ["DATA"]=> 
STRING(32) "9529C449206201FDBCE28CFA42FD48C8" } }

All I want is to just print the array variable "NAME" which is TEST to the screen. I'm hoping then I can workout how to pull out the user email in order to update my database. As you can see I'm a first time poster on Stack Overflow so apologies if my question is lacking. Thank you.

  • 写回答

1条回答 默认 最新

  • duanfei1987 2019-05-02 21:07
    关注

    How about:

    echo $_SESSION["user"]["name"];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作