我想在会话中存储我的查询结果,当用户刷新页面时,我想向他显示存储在会话中的相同结果。如果session为null,则再次查询,当用户离开页面会话时,应删除。我真正想做的是
if(!isset($_SESSION['result']))
{//if session is null then query database and store result in session
$query="SELECT * from table order by rand()";
$result=mysqli_query($connection,$query);
$_SESSION['result']=$result;
while ($read_all_data = mysqli_fetch_assoc($_SESSION['result']))
{
//echo data
}
else //if there is result in session then dont query just show me data
{
while ($read_all_data = mysqli_fetch_assoc($_SESSION['result']))
{
echo //same data
}
}
我做错了什么。我从database.i中选择随机行想要保留该顺序。按下刷新按钮
,您将无法获得新行答案 0 :(得分:2)
您正在存储mysqli_result
对象,并且无法对其进行序列化。您通常无法序列化外部资源。
将所有数据放入数组中,并将其存储在其位置。
if(!isset($_SESSION['result'])) {
$query = "SELECT * from table order by rand()";
$result = mysqli_query($connection, $query);
$all = array();
while ($row = mysqli_fetch_assoc($result)) {
$all[] = $row;
}
$_SESSION['result'] = $all;
}
foreach ($_SESSION['result'] as $data) {
// work with $data
}