Re: CSV TO MYSQL
| From: | Chris Hobbs | Date: | Tue, 28 Aug 2001 04:11:26 +0000 |
| Subject: | Re: CSV TO MYSQL | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-11871@lists.php.net to get a copy of this message | ||
Are you running php3 or php4? 'foreach' was added in php4, which might explain your error. If you're running php3, try replacing that line with:
while(list($key,$email) = each($emails)) {
I don't know if this works in php3 or not, but would be worth a shot.
Rupert L. Hussey wrote:
Hi Chris I get the following error when I try your script: Parse error in e:\myserver\www\maildump\dump2.php on line 3 Here is a copy of the script I am using...this may help some. An additional problem it has is that it is limited to 30 entries. Which is probably not efficient if you have a few hundred email address to transfer. Rupert ----------- It is GNU Licensed so no sweat --------------------------- <html> <head> <title>MyCSV-Dump for PHP</title> </head> <body bgcolor=ffffff> <? /* script configuration */ $dbname= "news"; // define database name $datetime = strftime('%d%m%Y'); // dateformat $filename_input = "addresses.txt"; // filename of input CSV file $filename_output = $dbname . ".sql"; // sample output filename: "test_16062001.sql" $debug_input = "yes"; // 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)) {-- Chris Hobbs Silver Valley Unified School District Head geek: Technology Services Coordinator webmaster: http://www.silvervalley.k12.ca.us/~chobbs/ postmaster: chobbs@silvervalley.k12.ca.us/* 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> ------snip!------------------------------------- "Chris Hobbs" <chobbs@silvervalley.k12.ca.us> wrote in message news:<3B8B0CF3.90205@silvervalley.k12.ca.us>...How about this: <?php $emailstring = "me@my.com;this@that.com;let@this.com"; @emails = explode(";", $emailstring); foreach($emails as $email) {getecho "INSERT INTO news VALUES('$email');\n";} ?> Rupert wrote:Ok, this is my issue - hope someone can help. Suppose I also need toit'sthis out of the way too - I am a Newbie (god..I hate that word...butdatabasetrue.....grin!) Anyway, I have a list of email address that I want to dump into athecalled (news) - eg. me@my.com this@that.com let@this.com there@here.com etc.. I separated them with (;'s) like so - me@my.com; this@that.com; let@this.com; there@here.com; etc.. (I suppose I could use commas instead - no prob there) This is what I want to accomplish: Each email address is a separate entry that needs to be downloaded intodatabase. Just like this: # -------------------------------------------------------- # # Table structure for table 'news' # CREATE TABLE news ( email char(255) NOT NULL, PRIMARY KEY (email), UNIQUE email (email) ); # # Dumping data for table 'news' # INSERT INTO news VALUES ( 'me@my.com'); INSERT INTO news VALUES ( 'this@that.com'); INSERT INTO news VALUES ( 'let@this.com'); INSERT INTO news VALUES ( 'there@here.com'); INSERT INTO news VALUES ( 'etc..'); ------------------------------------------------------------ I have a script that gets me to this point: INSERT INTO news VALUES (,"me@my.com"," this@that.com"," let@this.com","there@here.com","etc.."); But that is not what I want. I can attach the script for analysis or if anyone has a better idea. I would appreciated it very much. Thanks.. Rupert--Chris Hobbs Silver Valley Unified School District Head geek: Technology Services Coordinatorwebmaster: http://www.silvervalley.k12.ca.us/~chobbs/postmaster: chobbs@silvervalley.k12.ca.us