如何克隆 mysql_result
我会解释我的问题 从index.php我发布我的登录信息,并在login.php中我存储会话详细信息(如名称),例如:
$_SESSION['name'] = mysql_result($res,0,"name");
然后我被转发回index.php。 在这里,我想显示登录用户的名称:
echo $_SESSION['name']
以及数据库中的所有名称:
$result=mysql_query($query);
for ($i=0; $i<mysql_num_rows($result); $i++) {
$row = mysql_fetch_assoc($result);
$name =$row['name'];
echo $name;
}
问题来了:登录后,所有内容(用户名和所有名称)都正确显示。但是当我刷新页面时 echo $_SESSION['name']
显示数据库中的姓氏 == $name
而不是原来的
所以我想我需要存储到 $_SESSION['name'] 时克隆 mysql_result($res,0,"name");
非常感谢您
编辑 我在会话中存储的唯一内容是登录名。当该人登录时,他可以看到数据库中的所有人员。
I will explain my problem
From index.php I POST my login and in login.php I store session details (like name) e.g.:
$_SESSION['name'] = mysql_result($res,0,"name");
then I am forwarded back to index.php.
Here I want to show logged-in-user's name:
echo $_SESSION['name']
and also all names from the DB:
$result=mysql_query($query);
for ($i=0; $i<mysql_num_rows($result); $i++) {
$row = mysql_fetch_assoc($result);
$name =$row['name'];
echo $name;
}
And here comes the problem: after I log in, everything (both user's name and all names) is displayed correctly. But when I refresh the page echo $_SESSION['name']
shows the last name in the databse == $name
instead of the one that was
So I guess I need to clone mysql_result($res,0,"name");
when storing into $_SESSION['name']
Thank you very much
EDIT
the only thing i store in session is the login name. And when the person is logged-in he can see all people from database.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您正在寻找克隆对象,那么您可以尝试以下操作
从 php 文档看来,某些对象不支持克隆,因此如果是这种情况,那么您可以尝试以下操作(同样,来自 php 文档中的注释)进行深层复制
文档页面的注释中还提出了一些其他好的观点,可以在此处找到 http://php.net/manual/en/language.oop5.cloning.php
致以诚挚的问候
If you are looking to clone an object then you can try the following
From the php documentation it seems that some objects do not support cloning, so if this is the case then you could try the following (again, from comment in php documentation) to do a deep copy
There are some other good points made in the comments of the documentation page which can be found here http://php.net/manual/en/language.oop5.cloning.php
Best regards
在我看来,您最好不要在会话中存储所有其他名称。作为一个需要登录的数据库应用程序,我假设其他人可以使用和更新数据,这意味着每当他们更改某些内容时,您的会话保存的数据将在刷新时过时。
尝试让他们登录并设置他们的凭据,然后当他们返回索引时检查他们是否已登录并获取此时的名称。这样您就不必担心克隆提取,并且您拥有最新的信息。
In my opinion you are better off not storing all the other names in the session. Being a database app with logins I assume other people can use and update the data, meaning that any time they change something, your session-saved data will be obsolete on refresh.
Try just letting them login and set their on credentials, then when they get back to index check that they are logged in and fetch the names at that point. That way you don't have to worry about cloning the fetch AND you have the most up to date info.