Jump to content

Export mysql to txt / xml file and download it


thaidomizil

Recommended Posts

Hi, i have a table which shows some mysql data, every entry has a checkbox to select individual entries, now i want to be able to export those selected entries into a xml or txt file, i tried this:

 

 <?php
if($_POST['exporttxt']){
for($i=0;$i<count($_POST['checkbox']);$i++){
$export_id = $checkbox[$i];

$sql = "SELECT * FROM table WHERE id='$export_id'";
$result = mysql_query($sql);}


$output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<root>\n";
if($result->num_rows > 0)
{
while($myrow = $result->fetch_assoc())
{
$output .= "\t<row>\n";
foreach($myrow as $_name => $_value)
{
$output .= "\t\t<$_name>$_value</$_name>\n";
}
$output .= "\t</row>\n";
}
}
$output .= "</root>";
}

header('content-type: text/xml');
header('content-disposition: attachment; filename=data_export.xml');
echo $output;
exit;

?>

 

but that doesn't seem to work. Any hints ?

For one, mysql_query doesn't return an object, but your trying to use it's result as an object.

 

Second, your $result variable is only going to contain the data for the last select, all the rest are ignored.

 

Third, you can do everything in a single query rather than using a loop like your doing.  Put all the id's in an IN clause in the query.

 

I've got it to show the proper output atleast with all select boxes working, but it won't download the .txt file, i've tried with this headers and code:

 

<?php
if ($_POST['exporttxt']) {
for($i=0;$i<count($_POST['checkbox']);$i++){
   $export_id = $checkbox[$i];
$text = mysql_query("SELECT code FROM psc WHERE id='$export_id'");
$text = mysql_fetch_assoc($text);
$text = $text["code"];

ob_end_flush();
header("Content-type: text/plain");
header("Content-disposition: attachment;filename=\"filename.txt\"");
   header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");
    header("Content-Description: File Transfer");
    header("Content-Length: ".strlen($output).";\n");
    


echo($text);


}
}


?>

 

Like i said, the output itself is correct, but it won't offer me to download the .txt file.

You are ending the output buffer and clearing it, so $text is going to output nothing. Try this instead:

 

$text = ob_get_clean();

header("Content-type: text/plain");
header("Content-disposition: attachment;filename=\"filename.txt\"");
   header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");
    header("Content-Description: File Transfer");
    header("Content-Length: ".strlen($output).";\n");
    


echo($text);

  Quote

You are ending the output buffer and clearing it, so $text is going to output nothing. Try this instead:

 

 

The output on the screen is correct, but it still doesn't offer me any download, also tried with the code you suggested, same result.

Then something else is wrong ? If i select some rows and press my submit button, it submits the form, and shows me the output of what i selected i.e some codes which i actually want to write to the .txt file, i definitely included the headers.

In my code as well? Because in your code, you call ob_end_flush which is going to dump the buffer and output the results. Since there is output, the headers wouldn't have been sent and thus nothing will download.

 

But in my code, there is no output (unless it's coming from somewhere else). I'm guessing you just don't have errors on so you didn't notice all the errors from calling headers after output.

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.