本文介紹了使用 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 查詢結果的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!