Can anybody tell me why it does not seem to be possible to store the result returned by mysql_query in a session variable and then reuse it?
Suppose that I declare $dbResult to be a session variable, and then, on the first entry to the page I initialize it with a statement like this:
$dbResult = mysql_query($query,$link)
When I subsequently re-enter the page and attempt to use $dbResult, I get an error message that says:
Warning: Supplied argument is not a valid MySQL result resource
When I print out $dbResult on the first entry to the page, I get a value of
Resource id #2. However, when I re-enter the page, I get a value of 0!?!
Here is sample code illustrating the problem:
<?
if ( ! (isset($linkVar) ) )
{
session_start();
if ( ! session_is_registered("linkVar") ) {
session_register("LinkVar");
session_register("dbResult");
}
/
Make the mySQL connection ...
/
if (! ( $dbLink = mysql_pconnect("localhost","xxx","yyy") ) ) {
print("Failed to connect to the mySQL server!<BR>\n");
print("Abandoning connection attempt");
exit();
}
/
... and select the GOP Data Base
/
if ( ! mysql_select_db("dbase",$dbLink) ) {
print("Unable to use data base");
print("Abandoning attempt to use the data base!<BR>\n");
exit();
}
// Now store the $dbLink in a session variable
$linkVar = $dbLink;
}
if ( $submitted < 1)
{
$query="Select * from personal_info_table";
if ( ! $dbResult = mysql_query($query,$linkVar) )
{
print("Unable to query the People Table in the data base<br>\n");
print("MySQL reports the following error: " . mysql_error() . "<br>\n");
exit();
}
$row = mysql_fetch_array($dbResult,MYSQL_ASSOC);
}
else
{
print("I am in the second part of the routine");
}
?>
<form method="get" action=<? print($PHP_SELF) ?> >
<INPUT type="hidden" name="submitted" value="1" >
<?
print("The value of the dbResult variable is: " . $dbResult . "<br>\n");
$row = mysql_fetch_array($dbResult,MYSQL_ASSOC);
print("The value of the last name is: " . $row["last_name"] );
?>
<BR><BR>
<INPUT type="submit" value="Submit the Form">
</form>