WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : "myCSV-dump" converts a simple CSV-flatfile-database into an MySQL-dump.
Categories : PHP, MySQL, Databases, Complete Programs Update Picture
Thomas Fr?ch
Date : Jun 16th 2001
Grade : 2 of 5 (graded 29 times)
Viewed : 20040
File : 3125.zip
Images : No Images for this code example.
Search : More code by Thomas Fr?ch
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

MyCSV-Dump for PHP 1.0
----------------------

PURPOSE
-------

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;

/* read file */
$fcontents = file ($filename_input);

/* process each single line */
while (list ($line_num, $line) = each ($fcontents)) {

/* debug info (input) */
if ($debug_input == 'yes') {
         echo "<br><b>Line $line_num:</b> " . htmlspecialchars($line) . "<br>";
        }

        /* seperate line into array */
$without_space = trim (chop ($line));
$ln = explode ($seperator, $without_space);

        /* construct SQL query (max 30 entries - add more if you wish) */
        $sqlquery = 'INSERT INTO ' . $dbname . ' VALUES ('; // INSERT INTO test VALUES("1","0","abc");
        if ($ln[0]) { $sqlquery .= '"' . $ln[0] . '"'; }
        if ($ln[1]) { $sqlquery .= ',"' . $ln[1] . '"'; }
        if ($ln[2]) { $sqlquery .= ',"' . $ln[2] . '"'; }
        if ($ln[3]) { $sqlquery .= ',"' . $ln[3] . '"'; }
        if ($ln[4]) { $sqlquery .= ',"' . $ln[4] . '"'; }
        if ($ln[5]) { $sqlquery .= ',"' . $ln[5] . '"'; }
        if ($ln[6]) { $sqlquery .= ',"' . $ln[6] . '"'; }
        if ($ln[7]) { $sqlquery .= ',"' . $ln[7] . '"'; }
        if ($ln[8]) { $sqlquery .= ',"' . $ln[8] . '"'; }
        if ($ln[9]) { $sqlquery .= ',"' . $ln[9] . '"'; }
        if ($ln[10]) { $sqlquery .= ',"' . $ln[10] . '"'; }
        if ($ln[11]) { $sqlquery .= ',"' . $ln[11] . '"'; }
        if ($ln[12]) { $sqlquery .= ',"' . $ln[12] . '"'; }
        if ($ln[13]) { $sqlquery .= ',"' . $ln[13] . '"'; }
        if ($ln[14]) { $sqlquery .= ',"' . $ln[14] . '"'; }
        if ($ln[15]) { $sqlquery .= ',"' . $ln[15] . '"'; }
        if ($ln[16]) { $sqlquery .= ',"' . $ln[16] . '"'; }
        if ($ln[17]) { $sqlquery .= ',"' . $ln[17] . '"'; }
        if ($ln[18]) { $sqlquery .= ',"' . $ln[18] . '"'; }
        if ($ln[19]) { $sqlquery .= ',"' . $ln[19] . '"'; }
        if ($ln[20]) { $sqlquery .= ',"' . $ln[20] . '"'; }
        if ($ln[21]) { $sqlquery .= ',"' . $ln[21] . '"'; }
        if ($ln[22]) { $sqlquery .= ',"' . $ln[22] . '"'; }
        if ($ln[23]) { $sqlquery .= ',"' . $ln[23] . '"'; }
        if ($ln[24]) { $sqlquery .= ',"' . $ln[24] . '"'; }
        if ($ln[25]) { $sqlquery .= ',"' . $ln[25] . '"'; }
        if ($ln[26]) { $sqlquery .= ',"' . $ln[26] . '"'; }
        if ($ln[27]) { $sqlquery .= ',"' . $ln[27] . '"'; }
        if ($ln[28]) { $sqlquery .= ',"' . $ln[28] . '"'; }
        if ($ln[29]) { $sqlquery .= ',"' . $ln[29] . '"'; }
        $sqlquery .= ');';
        $sqlquery .= "\n"; // new line
                
        /* debug info (output) */
if ($debug_output == 'yes') {
                echo $sqlquery;
        }

        /* save SQL query to file (append) */
        if(file_exists($filename_output)) { // if file exists, only append sql query
                $save = fopen ($filename_output, "a");
                fputs($save, $sqlquery);
                flock($save,3);
                fclose($save);
                $save_status = "append";
        }
        else { // if file does not exist, write a txt header first
                $sqlheader = "# MyCSV-Dump for PHP 1.0\n";
                $sqlheader .= "# written by thomas.froehlich@uibk.ac.at\n";
                $sqlheader .= "# --------------------------------------\n\n\n\n";
                $sqlheader .= "#\n";
                $sqlheader .= "# Dumping data for table '" . $dbname . "'\n";
                $sqlheader .= "#\n\n\n";

                $save = fopen ($filename_output, "a");
                fputs($save, $sqlheader . $sqlquery);
                flock($save,3);
                fclose($save);
                $save_status = "newfile";
        }
        
}

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

</body>
</html>



This program allows you to upload an ODBC ressource - i.e. an MS-Access database to a MySQL server.
Categories : Databases, MySQL, Complete Programs, PHP, Databases
bookmarker - PHP, PHPLIB, MySQL WWW based bookmark manager
Categories : MySQL, PHP, MySQL, Complete Programs, Databases
Accepts a database & hostname from a user and then HTTP username and password. Uses this to connect to a MySQL database. Produces a form based on the tables it finds there to allow the user to do SELECTs, INSERTs, and DELETEs.
Categories : Databases, PHP, MySQL, Complete Programs
phpAds, a complete banner and ad management system with detailled tracking and stats.
Categories : MySQL, Complete Programs, Ecommerce, PHP, Databases
Point and Click Interface ala MS Access for creating SQL statements.
Categories : MySQL, Complete Programs, General SQL, PHP, Databases
Tropicalm Genetree Family (MySQL based family tree)
Categories : PHP, Interfaces, Databases, MySQL, Complete Programs
Browse a MySQL database & draw a tree view & load final items into a template page.
Categories : MySQL, Complete Programs, Algorithms, PHP, Databases
Shopping Basket On-Line Ordering System.
Categories : Complete Programs, MySQL, PHP, Ecommerce, Databases
free, search engine, indexing, system, information, web, ftp, http, free, software, cgi, php, MySQL, database, php3, FreeBSD, Linux, Unix, UdmSearch
Categories : MySQL, Complete Programs, PHP, Databases, Search
Example voting script. Lets people enter suggestions and vote for existing ones.
Categories : MySQL, PHP, Cookies, Complete Programs, Databases
Simple Mini Poll class library (SimPoll)
Categories : PHP, PHP Classes, Databases, MySQL, Complete Programs
complete, simple, working example of a login screen/system using php functions, cookies, and a mysql database for begginers.
Categories : Authentication, Complete Programs, PHP, MySQL, Databases
Shopping Cart e-Commerce Solution
Categories : Complete Programs, PHP, MySQL, Databases
AITSH Download
Categories : PHP, Complete Programs, MySQL, Databases
BBS system for easy customization. Utilizes mySQL.
Categories : Complete Programs, MySQL, PHP, Databases