php to export to csv over ssl wtf?!

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

mdemetri2
New php-forum User
New php-forum User
Posts: 1
Joined: Wed May 08, 2013 12:46 pm

php to export to csv over ssl wtf?!

Postby mdemetri2 » Wed May 08, 2013 12:49 pm

Ok, I have tried all day to get this to work. The best I have got to is that from page A, the user clicks download, which acts as a prompt to pass 'sqlcode' a variable holding a mysql query that was submitted by the user from selecting some values from drop down boxes, which have populated a html table - to page B that is supposed to take the query and results and spit out a csv file:

Code: Select all

<?php require_once('Connections/Connection1.php'); ?>
<?php
//create query to select as data from your table
$select = $_GET['sqlcode'];

//run mysql query and then count number of fields
$export = mysql_query ( $select )
or die ( "Sql error : " . mysql_error( ) );
$fields = mysql_num_fields ( $export );

//create csv header row, to contain table headers
//with database field names
for ( $i = 0; $i < $fields; $i++ ) {
$header .= mysql_field_name( $export , $i ) . ",";
}

//this is where most of the work is done.
//Loop through the query results, and create
//a row for each
while( $row = mysql_fetch_row( $export ) ) {
$line = '';
//for each field in the row
foreach( $row as $value ) {
//if null, create blank field
if ( ( !isset( $value ) ) || ( $value == "" ) ){
$value = ",";
}
//else, assign field value to our data
else {
$value = str_replace( '"' , '""' , $value );
$value = '"' . $value . '"' . ",";
}
//add this field value to our row
$line .= $value;
}
//trim whitespace from each row
$data .= trim( $line ) . "\n";
}
//remove all carriage returns from the data
$data = str_replace( "\r\n", "", $data);


//create a file and send to browser for user to download
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"excelfile.csv\";" );
header("Content-Transfer-Encoding: binary");

print "$header\n$data";
exit;
?>


However, what I get is IE(v8) seeing it as a HTML file and displaying all the text in the browser window....any ideas??

Return to “PHP coding => General”

Who is online

Users browsing this forum: kstartl83 and 2 guests