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"];
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题