PHP conversion of CSV to SQL

Hey found this prog online, and twisted it a bit to fit my needs but I am having a problem and perhaps someone can explain what I am doing wrong.

[php]

<? /* script configuration */ $dbname= "slkeys"; // define database name $datetime = strftime('%d%m%Y'); // dateformat $filename_input = "numbers.csv"; // 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 = "
";
$htmlheader .= "# ";
$htmlheader .= "# ";
$htmlheader .= "# ";
$htmlheader .= "# Dumping data for table '" . $dbname . "' from file '" . $filename_input . "'
"; $htmlheader .= "
";
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 "
Line $line_num: " . htmlspecialchars($line) . "
"; } /* 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[2] . '"'; } 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') { } /* 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 = “# n”;
$sqlheader .= “# n”;
$sqlheader .= “# nnnn”;
$sqlheader .= “#n”;
$sqlheader .= “# Dumping data for table '” . $dbname . “'n”;
$sqlheader .= “#nnn”;
$save = fopen ($filename_output, “a”);
fputs($save, $sqlheader . $sqlquery);
flock($save,3);
fclose($save);
$save_status = “newfile”;
}
}
echo “

”;
/* output success information */
if ($save_status == “append”) {
echo "DONE: Data hasbeen appended to existing file: " . $filename_output .
”;
}
if ($save_status == “newfile”) {
echo "DONE: Data hasbeen written to new file: " . $filename_output . “”;

}

?>

[/php]

sample input

c4ca97f3-9c84-4f9e-b001-6d8a41fa5156,00 Allen
061b4333-ad42-4715-94b1-3e7a61cc2af5,00 Antfarm
643177c1-8bcb-49ae-89ba-ee816a1600fb,00 Anzac
7bf38cc6-7019-427d-946a-72fae45d19fc,00 Back
920f2b76-63ff-44f5-8f13-ca2f6558c3e9,00 Bamboo
173636b3-cdeb-491e-a6c2-dbd3647f669a,00 Goombah

sample output

INSERT INTO slkeys VALUES ("09d769f6-9568-4712-8468-a648f4dc1a49","");
INSERT INTO slkeys VALUES ("429c0039-ce5b-450d-98c5-bc44d0c86c2c","");
INSERT INTO slkeys VALUES ("91b71273-1a9d-4149-b0b1-51d76f739477","");
INSERT INTO slkeys VALUES ("721be6d4-08c7-4339-ae39-d5f8592a8626","");
INSERT INTO slkeys VALUES ("792ec162-c612-4579-a2c6-551f8891a85c","");
INSERT INTO slkeys VALUES ("95b55aa8-329f-4648-ba0a-eb306977ad6d","");
INSERT INTO slkeys VALUES ("4bab9f23-dd94-4fb9-92d8-bb81f40a904d","");
INSERT INTO slkeys VALUES ("57f1739c-bada-442b-9a67-48be49e594a9","");
INSERT INTO slkeys VALUES ("c029feb3-4cec-48c4-85e0-e8fc976c3452","");

ideal output

INSERT INTO slkeys VALUES ('c029feb3-4cec-48c4-85e0-e8fc976c3452','00 Allen');

can anyone shed some light on this? Thanks

   Draco

if ($ln[0]) { $sqlquery .= ‘"’ . $ln[0] . ‘"’; }
if ($ln[1]) { $sqlquery .= ‘,"’ . $ln[2] . ‘"’; }
if ($ln[2]) { $sqlquery .= ‘,"’ . $ln[2] . ‘"’; }

ROTFL! Thanks Q! Hopefully others can use the prog as well, I plan to soup it up in my free time Ill post the finished product and a link to a hosted version here :)

Sponsor our Newsletter | Privacy Policy | Terms of Service