mysql 查session,使用PHP SESSION变量存储MySQL查询结果

I want to execute a query in one file, store it in a session variable, and access the result in another file.

File1.php:

//This is the file I have executed my sql statement in

//I have already done session_start(); and initialized $conn

$query = //my query. I know it is working because I have tested it elsewhere, so I

// don't believe it is the problem

$_SESSION['query2'] = mysqli_query($conn, $query)

File2.php:

//This is the file I want to access my query results in.

//I have already done session_start(); and initialized $conn

$tempArray = $_SESSION['query2'];

if (isset($tempArray)) {

$row = mysqli_fetch_array($tempArray, MYSQL_NUM);

if ($row == null) {

echo "

error

"; //This line gets executed

} else {

echo "

works!

";

}

} else {

echo "

array not set

";

}

Any ideas as to what I am doing wrong? Or, is there another way to accomplish what I'm trying to do here?

解决方案

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值