Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is it possible to export queried data into Excel or other

Status
Not open for further replies.

Bravogolf

Programmer
Nov 29, 2002
204
GB
As part of an admin reporting view, there are two or three reporting templates I would like to setup (i.e. three predefined PHP scripts that run a report of an MySQL server).

Is it possible, that when the data is returned, or at any stage even, to export this data into a file for offline viewing? In any format, but Excel (CSV etc) preferably.

 
Here is a cut down example from a script I wrote a long time ago. You would insert a query and build up the results from a result set (where the comment indicates).
Code:
<?
$my_data = '"ID","Name","Phone number"'."\n";

/* loop through your recordset and build up a valid CSV file
   into the $my_data variable
...
*/

$output_file = 'MyExportedData.csv'; 
if(ini_get('zlib.output_compression')) ini_set('zlib.output_compression', 'Off');
header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT'); 
header('Cache-Control: no-store, no-cache, must-revalidate'); // HTTP/1.1 
header('Cache-Control: pre-check=0, post-check=0, max-age=0'); // HTTP/1.1 
header('Content-Transfer-Encoding: none'); 
header('Content-Type: application/octetstream; name="' . $output_file . '"'); //This should work for IE & Opera 
header('Content-Type: application/octet-stream; name="' . $output_file . '"'); //This should work for the rest 
header('Content-Disposition: attachment; filename="' . $output_file . '"'); 

echo $my_data; 
exit();
?>
There's probably a function or a free class out there to do all this for you [smile]

Hopefully this works for you!

Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
Wow, that was quick. That's brilliant, thanks Jeff!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top