Training, Open Source
computer languages


PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 

Search our site for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
mysql dump into a text file

Posted by bschultz (bschultz), 17 September 2003
is there an easy way to dump a mysql db table into a text file, that is in a format suitable for reading?  (for example - one column per line...etc)

Posted by admin (Graham Ellis), 17 September 2003
You can select into outfile ...

select * from course into outfile "/tmp/fred.txt";

This will give you a tab delimited file, one row per row of  the table. Options are available to let you change the delimiter.  I suggest you give a full absolute path, or the output file will be dumped (I think) in the MySQL user's home directory.

If you want to transpose rows and columns, it's not straightforward - I read it that you want to do that, but perhaps I've misread the question?

Posted by bschultz (bschultz), 18 September 2003
using the following code...that didn't work right.  It put everything on the first "value", since it's tab delimited.  

Code:
<?php

$fp = @fopen("results.txt", "rb") or die("Couldn't open file");
@$data = fread($fp, filesize($fp));

while(!feof($fp))
{
$data .= fgets($fp);
}

fclose($fp);

$values = explode("\r\n", $data);

echo "$values[0]";
echo "$values[1]";

?>


I thought (obviously wrong!) that the

Code:
explode("\r\n", $data);


would put each value on a new line.  What am I doing wrong?

Posted by admin (Graham Ellis), 19 September 2003
You're reading the whole file with your fread, so there's nothing left for your while loop and fgets.  Have a look at the MySQL docs for the "into outfile" stuff too - there's options you can use to change separators and things ike that whihc might format the data better for your particular requirement.

Posted by bschultz (bschultz), 19 September 2003
thanks, Graham.  I was looking (without any luck) at the php.net docs.

Brian



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.


WELL HOUSE CONSULTANTS LTD.: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho