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 ?

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.