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
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

Link to comment
Share on other sites

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'];

Link to comment
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.