I have a form on a webpage which is to be used to filter through a database depending on what the user enters:
An Ajax call to a database returns the count of properties in real time which are satisfied by the query:
$query = "
SELECT
*
FROM
first_page_data
WHERE
((((acos(sin((".$lat."*pi()/180)) * sin((`geo_lat1`*pi()/180))+cos((".$lat."*pi()/180)) * cos((`geo_lat1`*pi()/180)) * cos(((".$lon."- `geo_lon1`)* pi()/180))))*180/pi())*60*1.1515) * 1.609344) < ".$range."
AND
value_min >= " . $valmin . "
AND
value_max <= " . $valmax . "
AND
bed_min >= " . $bedmin . "
AND
bed_max <= " . $bedmax;
$result = mysql_query($query, $first_data);
$number = mysql_num_rows($result);
echo $number;
This works fine, and all works as I want. However I now want to do "something" with the results (eg show them all in a table when a button is clicked, show them on a google map, etc). How do I store the results of this query for use on another webpage? I cant re-query the database on another page because the results are dependant on the users input on this form.
You can store them in a temporary session. I actually do not advocate this approach but without knowing your entire env should provide a resolution.
$result = mysql_query($query, $first_data);
while($row = mysql_fetch_assoc($result)) {
$_SESSION['store'][] = $row;
}
I would also highly suggest migrating to using mysqli if you can and once you have finished with this you can wipe it out by using
unset($_SESSION['store']);
If your result set is massive I do not recommend storing it in a temp session. Store it into a file as a json array.
while($row = mysql_fetch_assoc($result)) {
$rows[] = $row;
}
file_put_contents($unique_var . '.txt', json_encode($rows));
You will need to set a unique value that so you can retrieve the correct file later. Wipe out the file once finished with by using:
unlink($unique_var . '.txt');
So what about future proofing this? SESSION storage is easier but with a massive result set and a lot of users this could be very resource hungry, so try this:
$result = mysql_query($query, $first_data);
while($row = mysql_fetch_assoc($result)) {
$rows = $row;
}
if(count($rows) < 51) {
$_SESSION['store'] = $rows;
} else {
file_put_contents($unique_var . '.txt', $rows);
}
And you could later access it like this:
if(!isset($_SESSION['store'])) {
$contents = file_get_contents($unique_var . 'txt');
unlink($unique_var . '.txt');
} else {
$contents = $_SESSION['store'];
unset($_SESSION['store']);
}
This way you will always purge the session and temp file as quickly as possible.
Why don't you just use a session to store the user's input and then re-query the database. No need to store all of the results in a session.