使用 PHP SESSION 变量存储 MySQL 查询结果 [英] Using PHP SESSION Variables to store MySQL query results

查看:38
本文介绍了使用 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 "<h1> error </h1>"; //This line gets executed
    } else {
        echo "<h1> works! </h1>";
    }
} else {
    echo "<h1> array not set </h1>";
}

对我做错了什么有任何想法吗?或者,有没有其他方法可以完成我在这里尝试做的事情?

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

这篇关于使用 PHP SESSION 变量存储 MySQL 查询结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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