开发者

how to clone mysql_result

开发者 https://www.devze.com 2023-03-14 05:35 出处:网络
I will explain my problem From index.php I POST my login and in login.php I store session details (like name) e.g.:

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开发者_JS百科 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.


If you are looking to clone an object then you can try the following

$object2 = clone $object1;

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

protected function deepCopy($inputObject) {
    return unserialize(serialize($inputObject)); 
}

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消