I script it as follows,
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>";
}
The first page access goes well, i.e. accessing from db. But when I try to fetch from $_SESSION in 2nd page request, it says ...
"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) {
Any idea?
If resultset returned by php is connected resource (like Java), why we can still iterate it after closing the connection with DB?
When I call get_resource_type($employees)
... it says, its an object, not a resource. Then why an object is not being stored in session?