Jump to content

Storing last query in a session


dmccabe

Recommended Posts

I have built a database, which allows the user to run a query on 1 field or on a combination of fields.

 

I want to make it so they can run the query as they need it, it displays the info on the page (up to here is already working), but then there will be a button to export the results to Excel.

 

I already have a script to export to excel, but I need to pass the query string to the script, but how can I capture the last query string and then pass it to this sheet?

 

Obviously I can use sessions, but I am not sure how best to capture the query string.

Link to comment
https://forums.phpfreaks.com/topic/144488-storing-last-query-in-a-session/
Share on other sites

This will add the returned vars to the session

 

<?php session_start();
$sql = "SELECT * FROM table ORDER BY id DESC LIMIT 1";
$result = mysql_query($sql);
if ($result_row = mysql_fetch_array($result)) {
$_SESSION['name'] = $result_row["name"];
$_SESSION['data'] = $result_row["data"];
$_SESSION['userID'] = $result_row["userID"];
}
header('Location: next_file.php');
?>

 

Hope that helps

Thanks for the help, but I dont fully understand what you mean?

 

I have to specify the fields each time?  I want it to remember the query as it was run last, so then it can pass that query to the excel export script.

 

Excel Export Script:

<?php
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=branchlist.xls");
header("Pragma: no-cache");
header("Expires: 0");
include($_SERVER['DOCUMENT_ROOT'] . 'connect2.php');
//mysql_connect(localhost,$username,$password);
//@mysql_select_db($database) or die("Unable to select database");
$select = "SELECT `name`, `code`, `address`, `post_code`, `front_line`, `back_line`, `fax`, `mobile` FROM tbl_branches WHERE `live` = '1' ORDER BY `name` ASC";
$export = mysql_query($select);
$count = mysql_num_fields($export);
for ($i = 0; $i < $count; $i++) {
	$header .= mysql_field_name($export, $i)."\t";
}
while($row = mysql_fetch_row($export)) {
	$line = '';
	foreach($row as $value) {
		if ((!isset($value)) OR ($value == "")) {
			$value = "\t";
		} else {
			$value = str_replace('"', '""', $value);
			$value = '"' . $value . '"' . "\t";
		}
		$line .= $value;
	}
	$data .= trim($line)."\n";
}
$data = str_replace("\r", "", $data);
if ($data == "") {
	$data = "\n(0) Records Found!\n";
}
print "$header\n$data";
?> 

 

so what I would like to do is pass the query in where it currently has:

 

$select = "SELECT `name`, `code`, `address`, `post_code`, `front_line`, `back_line`, `fax`, `mobile` FROM tbl_branches WHERE `live` = '1' ORDER BY `name` ASC";

 

so instead it says something like:

 

$select = $_SESSION['LAST_QUERY'];

  • 2 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.