Jump to content


Photo

Export Data to Textfile


  • Please log in to reply
5 replies to this topic

#1 lilywong

lilywong
  • Members
  • PipPipPip
  • Advanced Member
  • 56 posts

Posted 27 July 2006 - 03:57 AM

Any idea of exporting a mysql data to a textfile, saved it as file.txt ?



#2 Branden Wagner

Branden Wagner
  • Members
  • PipPipPip
  • Advanced Member
  • 111 posts

Posted 27 July 2006 - 04:01 AM

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?

#3 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 27 July 2006 - 04:01 AM

Look at mysqldump.
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#4 lilywong

lilywong
  • Members
  • PipPipPip
  • Advanced Member
  • 56 posts

Posted 31 July 2006 - 01:43 AM

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.

#5 lilywong

lilywong
  • Members
  • PipPipPip
  • Advanced Member
  • 56 posts

Posted 31 July 2006 - 02:06 AM

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

#6 Drumminxx

Drumminxx
  • Members
  • PipPipPip
  • Advanced Member
  • 92 posts
  • LocationUSA

Posted 31 July 2006 - 02:21 AM

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

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

:: gucci.com ::
:: blockbuster.com ::

--------------------




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users