在会话中存储mysqli_query结果 [英] store mysqli_query result in session

查看:45
本文介绍了在会话中存储mysqli_query结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将MySQLi查询的结果存储为会话变量,以便我可以重用它而无需再次执行查询.我不想在网站的每个页面上或每次刷新页面时都执行相同的查询.

I want to store the result of a MySQLi query as a session variable so that I can reuse it without executing the query again. I don't want to execute the same query on every page of my website or every time a page is refreshed.

我已经尝试了下面的代码,但是遇到诸如对象无法存储在会话中"和"mysqli_fetch_array期望参数1为资源"之类的错误.

I've tried the code below, but I get errors like "object can not be stored in session" and "mysqli_fetch_array expects parameter one to be a resource".

如何将查询结果存储在会话中?

How can I store the query result in a session?

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 */
}

推荐答案

尝试一下:

  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);
}

这篇关于在会话中存储mysqli_query结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆