我编写如下脚本,
session_start();
$employees = null;
if (isset($_SESSION['employees'])) {
$employees= $_SESSION['employees'];
} else {
$con = new mysqli("localhost", "root", "test", "emp_db");
$employees = $con->query("SELECT * from employee");
$_SESSION['employees'] = $employees;
}
echo "<table>";
foreach ($employees as $row) {
echo '<tr>';
echo "<td>" . $row['emp_id'] . " </td>";
echo "<td>" . $row['emp_name'] . " </td>";
echo "<td>" . $row['emp_address'] . "</td>";
echo "</tr>";
}
第一页访问顺利,即从db访问。但是当我尝试在第二页请求中从 $_SESSION 中获取时,它说...
"Warning: main(): Couldn't fetch mysqli_result in C:\xampp\htdocs\test\dbtemp.php on line 22". My line 22 is: foreach( $employees as $row) {
任何想法?
如果php返回的resultset是已连接的资源(如Java),为什么我们在关闭与DB的连接后仍然可以对其进行迭代?
当我打电话时get_resource_type($employees)
......它说,它是一个对象,而不是一个资源。那么为什么一个对象没有被存储在会话中呢?