Jump to content

Archived

This topic is now archived and is closed to further replies.

lilywong

Export Data to Textfile

Recommended Posts

how much data? and whats its purpose? just exporting the results to a text file, or the actual table itself, as in a back up?

Share this post


Link to post
Share on other sites
just want to export some data, possibly 100 records from database, and save in abc.txt, that's it. not for backup, is for a report. like CSV file.

Share this post


Link to post
Share on other sites
i found a solution of this, but the data output is all in one line, i have  use the "\n", but everything still printed out in one line, just wondering, i wanna the record to be printed out in many lines based on different ID.


for ($i = 0; $i < $fields; $i++) {
    $header .= mysql_field_name($export, $i) . "|";
}

while($row = mysql_fetch_row($export)) {
    $line = '';
    foreach($row as $value) {
        if ((!isset($value)) OR ($value == "")) {
            $value = "|";
        } else {
            //$value = str_replace('|', '', $value);
            $value = "|".$value."|";
        }
        $line .= $value;
    }
    //$data .= trim($line). "\n";
    $data .= $line."\n";
}
//$data = str_replace("\r","",$data);

if ($data == "") {
    $data = "\n(0) Records Found!\n";
}

header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=extraction.txt");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";

Share this post


Link to post
Share on other sites
if you just want a simple text file, you could just loop through the records you want and build a string formatted the way you want and then the code below is an example of how to create the actual text file

[code]
$fh = fopen('FILE_PATH', 'w') or die("ERROR: can't open file for writing");
fwrite($fh, $NEW_FORMATTED_STRING);
fclose($fh);
[/code]

Share this post


Link to post
Share on other sites

×

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.