使用PHP会话变量存储MySQL查询结果
我想在一个文件中执行查询,将其存储在会话变量中,然后在另一个文件中访问结果 File1.php:使用PHP会话变量存储MySQL查询结果,php,mysql,mysqli,session-variables,Php,Mysql,Mysqli,Session Variables,我想在一个文件中执行查询,将其存储在会话变量中,然后在另一个文件中访问结果 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
//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>";
}
//这是我要在其中访问查询结果的文件。
//我已经完成了会话_start();初始化$conn
$tempArray=$\会话['query2'];
if(isset($tempArray)){
$row=mysqli\u fetch\u数组($tempArray,MYSQL\u NUM);
如果($row==null){
echo“error”;//执行此行
}否则{
回声“工作!”;
}
}否则{
回显“未设置阵列”;
}
你知道我做错了什么吗?或者,是否有其他方法来完成我在这里尝试的操作?在会话中存储实际阵列:
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);
你有两个问题:
\uuu sleep()
和\uu wakeup())
因此,您可以选择如何序列化对象-序列化中将包含哪些成员,以及在反序列化时如何重新初始化其他成员-还请记住,当您取消序列化此类对象时,必须定义对象的类
正如前面所说,您的最终解决方案将是获取行并将它们保持在会话中。如果您不想保留行(您告诉我只有一行,但可能您不想急于获取),请保留一个包含要定义的数据的数组,然后再次绑定查询。Awesome!这帮助很大,但不幸的是,有时我的数据会有多行。。我应该如何运行一个循环来完成我的任务?@user2771279用solutionhmm编辑,这对我来说似乎是无限的。假设它是mysqli_fetch_数组(mysqli_query($conn,$query))?@user2771279是的,愚蠢的错误。很抱歉,我会编辑的!