php将结果存入会话中,PHP-在会话中存储mysqli_query结果

我想将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 */

}

解决方法:

尝试这个:

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

}

标签:php,mysqli,session-variables

来源: https://codeday.me/bug/20191014/1911579.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值