Using PHP SESSION Variables to store MySQL query results [duplicate]

拈花ヽ惹草 提交于 2019-12-02 07:24:42

Store the actual array in session:

File1.php:

$query = //my query. 
$result = array();
$res = mysqli_query($conn, $query);
while($row = mysqli_fetch_array($res, MYSQL_NUM){
    $result[] = $row;
}
$_SESSION['query2'] = $result;

File2.php:
//I have already done session_start(); and initialized $conn

$tempArray =  $_SESSION['query2'];
var_dump($tempArray);

You have two problems there:

  1. Trying to store a class instance is as they told you (remember that the class should implement __sleep() and __wakeup() so you can choose how to serialize the object - what members will you include in serialization, and how will you re-initialize other members when you deserialize - also remember that the object's class must be defined when you unserualiza such object).
  2. Trying to store, particularly, a myqsli object. Those objects contain references to resources which can be collected, so even if you successfully serialize-back such object, their internal references could not be valid anymore.

Your end solution will be, as said before, fetching the rows and keeping them in session. If you don't want to keep the rows (you told there's only one row, but perhaps you don't want an eager-fetching), keep an array with the data to define and bind the query again.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!