Convert a simple CSV-flatfile-database into an MySQLdump (file containing
SQL-queries), so that you can import it easily into your MySQL-database.
INSTALLATION
------------
1. Be sure to have PHP installed correctly.
2. Copy "csvdump.php" to a place where it can be executed (eg: webserver-root, etc)
3. Edit "csvdump.php" with a text editor and change the variables
mentioned under "/* script configuration */"
- seperator string
- CSV input filename
- MySQL database name
- debugging options
USAGE
-----
1. Run "csvdump.php" either from the command line or in your browser to
have a .SQL-file generated in the same directory.
2. If you don´t already have a database and table to import the data into, then create now
- a database
- a table with the same number of fields like your CSV-file
(be sure to define the field types according to your imported data)
Please note, that the order, in which the fields are converted, stays exactly the same.
3. Import the generated .SQL-file into MySQL, either from the command line
mysql your_dbname < test_01012001.sql
or use another tool like MyPHPadmin to import the data.
CREDITS
-------
Version 1.0 by Thomas Fröhlich (thomas.froehlich@uibk.ac.at)
Use it at your own risk, i give no warrantly of any kind.
Dedicted to the great PHP & open source community!
Released under the LGPL. Change it if you want, but please
be so kind to send me a notification if you add some
functionality. thx!
<html>
<head>
<title>MyCSV-Dump for PHP</title>
</head>
<body bgcolor=ffffff>
<?
/* script configuration */
$dbname= "test"; // define database name
$datetime = strftime('%d%m%Y'); // dateformat
$filename_input = "flatfile.txt"; // filename of input CSV file
$filename_output = $dbname . "_" . $datetime . ".sql"; // sample output filename: "test_16062001.sql"
$debug_input = "no"; // yes = show file input
$debug_output = "yes"; // yes = show generated SQLquery
$seperator = "|"; // symbol used as CSV field seperator
/* output html page header */
$htmlheader = "<pre>";
$htmlheader .= "# MyCSV-Dump for PHP 1.0<BR>";
$htmlheader .= "# by Thomas Fröhlich (thomas.froehlich@uibk.ac.at)<BR>";
$htmlheader .= "# ------------------------------------------------<BR>";
$htmlheader .= "# Dumping data for table '" . $dbname . "' from file '" . $filename_input . "'<BR>";
$htmlheader .= "</pre><PRE>";
echo $htmlheader;
echo "</PRE>";
/* output success information */
if ($save_status == "append") {
echo "<font color=green><B>DONE:</B> Data has been appended to existing file: <B>" . $filename_output . "</B>";
}
if ($save_status == "newfile") {
echo "<font color=green><B>DONE:</B> Data has been written to new file: <B>" . $filename_output . "</B>";
}
?>