简体   繁体   English

PHP MYSQL存储会话数据,问题

[英]PHP MYSQL storing session data, question

I would like to populate my $_SESSION array with all DB data of the relevant user that just logged in. 我想用刚刚登录的相关用户的所有数据库数据填充$ _SESSION数组。

like "FIRST_NAME" in the DB would be $_SESSION["FIRST_NAME"]; 就像数据库中的“ FIRST_NAME”是$ _SESSION [“ FIRST_NAME”];

What is the way to do that properly ? 正确执行此操作的方法是什么?

$_SESSION = $result_set; $ _SESSION = $ result_set; //$result_set is the fetched array from the DB // $ result_set是从数据库中获取的数组

Yielded an error (unknown() at line 0) 产生错误(第0行的unknown())

thanks in advance! 提前致谢!

I would like to populate my $_SESSION array with all DB data of the relevant user that just logged in. 我想用刚刚登录的相关用户的所有数据库数据填充$ _SESSION数组。

Don't do this. 不要这样 Store only the row ID in the session, and do a database request any time you need some information. 在会话中仅存储行ID,并在需要任何信息时执行数据库请求。

Storing this information in the session is completely redundant, and may lead to outdated data if the user changes something in the database that isn't reflected in the session variable. 在会话中存储此信息是完全多余的,如果用户更改了数据库中未反映在会话变量中的某些内容,则可能会导致数据过时。

You need to specify a name for the data you're storing - you can't overwrite the $_SESSION variable completely. 您需要为要存储的数据指定一个名称-您不能完全覆盖$ _SESSION变量。

EG: 例如:

$_SESSION['user_id'] = $result_set->id;

Just as Pekka said, there's rarely the need to store the entire user profile in the session when you can do a separate database call to gather this data later. 正如Pekka所说,当您以后可以进行单独的数据库调用以收集此数据时,几乎不需要在会话中存储整个用户配置文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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