Is there some way to send the results of a query (as an array) to another page via POST?
<?
$qry_string = "Select * from table where user_id=1";
$result = mysql_query($qry_string);
?>
Now, usually I would go through and retrieve the data from each row returned by the query:
while($row = mysql_fetch_assoc($result))
{
echo $row['some_column'];
}
However, I want to run the above line from another page, therefore the returned query results will need to be sent to that page. How should I go about doing this? I’ve tried to serialize and unserialize the array by using the following:
<input type="hidden" value="<? print_r(serialize($project_data)) ?>" name="project_data" />
However, when I go to unserialize the array:
$project_arr = unserialize(stripslashes($_POST['project_data']));
print_r($project_arr); // RETURNS NULL
// Error thrown here
foreach ($project_arr as $row) {
$project_name = $row['project_name'];
$project_type = $row['project_type'];
}
I get the following error:
Warning: Invalid argument supplied for foreach()
What is the correct way to send a query result to another PHP page, and read the row’s data in?
You can’t serialize the object directly, as not all the data from the query has been fetched yet. You can, however, fetch all the data into an array, then serialize that: