哪个是最好的方法?将用户名保存在会话中或将用户的 ID 保存在会话中?
用户 ID 或用户名最好保存在会话中,也保存在 mysql 中,这是最好的方法。
--> “从用户表中选择*,其中loginname =”。 $_SESSION['用户名'];
或
--> “从用户表中选择*,其中id =”。 $_SESSION['id'];
无论是整数比较还是字符串比较,这是SQL中最好的比较。
user id or username which is better to save in session and also in mysql which is the best way.
--> "select * from usertable where loginname=" . $_SESSION['username'];
or
--> "select * from usertable where id=" . $_SESSION['id'];
whether integer comparison or string comparison which is the best comparison in SQL.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您通常应该保存 ID,因为如果您正在运行完全规范化的数据库,那么在执行其他查找时,这是对您最有用的值。
You should usually save the ID, because if you are running a fully normalised database this is the value that is most useful to you when performing other lookups.
你的想法对我来说真的没有多大意义,真的!
整数比较或字符串比较——为什么你认为其中一个比另一个更快?当然,如果你逐条指令进行整数比较会更快,但你为什么要这样做呢?
我的回答是:坚持你想要的。选择一个保证是独一无二的。另外,选择用户名并不是一件好事。为每个登录的用户生成唯一的 ID。并将该 ID 存储到
$_SESSION[..]
中。显然,您需要某种机制来验证 ID,并将其与用户名关联起来。What you are thinking doesnt really make much sense to me, really!
Integer comparison or String comparison -- Why do you think one will be faster that the other? Of course, if you go instruction by instruction Integer comparison will be faster, but why do you want that?
My answer: Stick to what ever you want. Choose the one that will be guaranteed to be unique. Plus, choosing username, isnt something great. Generate a unique ID for every user that is logged on. And store that ID into the
$_SESSION[..]
. Obviously, you would need to have some mechanism to validate the ID, and associate it with the username.整数比较总是比字符串更准确,您还需要考虑这样一个事实,每个用户的 id 都是唯一的,并且可能有多个用户具有相同的用户名,这取决于您的系统。
我相信 id 比较是最好的方法
Integers comparison is always more accurate than strings, also you would like to consider the fact, the id is unique for each user, and there maybe an several user with the same username, that depends on your system.
I believe id comparison is the best way to go