我想将MySQLi查询的结果存储为会话变量,以便我可以重用它而无需再次执行查询。我不希望在我的网站的每个页面或每次刷新页面时执行相同的查询。
我已经尝试了下面的代码,但是我得到的错误就像“对象无法存储在会话中”和“mysqli_fetch_array期望参数1成为资源”。
如何将查询结果存储在会话中?
session_start();
if (!isset($_SESSION['query_result'])) {
$anything=mysqli_query($connection,"select something from table name
where filed1='$variable' order by id desc limit 70");
$_SESSION['query_result']=$anything;
} else {
$anything= $_SESSION['query_result'];
}
while ($data=mysqli_fetch_array($anything)) {
/* output the data */
}
发布于 2018-08-30 15:21:39
尝试这个:
if(!isset($_SESSION['query_result'])){
$anything=mysqli_query($connection,"select something from table name where filed1='$variable' order by id DESC limit 70");
while($res = mysqli_fetch_row($anything)){
array_push($_SESSION['query_result'], $res);
}
} else {
$anything = $_SESSION['query_result'];
}
while($data=mysqli_fetch_array($anything)){
print_r($data);
}
发布于 2018-08-30 15:58:32
如果您不仅要为一个客户端存储数据,而且还要在服务器端存储数据,我建议您查看如何在php中构建内存服务器端缓存?。一些框架已经内置了这个。
否则使用其中一个mysqli API方法,如http://php.net/manual/de/mysqli-result.fetch-assoc.php,这应该返回一个数组而不是一个对象。
mysqli_query()返回一个你无法存储到session中的对象(有一个将对象转换为数组的方法,但我不建议你存储整个对象)http://php.net/manual/de/ function.get-对象vars.php
https://stackoverflow.com/questions/-100002504
复制相似问题