本文共 1229 字,大约阅读时间需要 4 分钟。
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);
转载地址:https://blog.csdn.net/weixin_34236334/article/details/118814960 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!