Hi I got the below code of the net, great job, only its not importing the data to the database can someone help me out to see what I might be missing out on.
I don’t get an error message or anything, it just doesn’t seem to add anything for me
Thanks
<?php
$databasehost = "test";
$databasename = "test";
$databasetable = "test";
$databaseusername ="test";
$databasepassword = "test";
$fieldseparator = ",";
$lineseparator = "\
";
$csvfile = "LOCATION OF CSV FILE";
$addauto = 1;
$save = 0;
$outputfile = "output.sql";
if(!file_exists($csvfile)) {
echo "File not found. Make sure you specified the correct path.\
";
exit;
}
$file = fopen($csvfile,"r");
if(!$file) {
echo "Error opening data file.\
";
exit;
}
$size = filesize($csvfile);
if(!$size) {
echo "File is empty.\
";
exit;
}
$csvcontent = fread($file,$size);
fclose($file);
$con = @mysql_connect($databasehost,$databaseusername,$databasepassword) or die(mysql_error());
@mysql_select_db($databasename) or die(mysql_error());
$lines = 0;
$queries = "";
$linearray = array();
foreach(split($lineseparator,$csvcontent) as $line) {
$lines++;
$line = trim($line," \ ");
$line = str_replace("\\r","",$line);
$linearray = explode($fieldseparator,$line);
$linemysql = implode("','",$linearray);
if($addauto)
$query = "insert into $databasetable values('','$linemysql');";
else
$query = "insert into $databasetable values('$linemysql');";
$queries .= $query . "\
";
@mysql_query($query);
}
@mysql_close($con);
if($save) {
if(!is_writable($outputfile)) {
echo "File is not writable, check permissions.\
";
}
else {
$file2 = fopen($outputfile,"w");
if(!$file2) {
echo "Error writing to the output file.\
";
}
else {
fwrite($file2,$queries);
fclose($file2);
}
}
}
connect_db();
$q="LOAD DATA
INFILE 'QTest.csv'
INTO TABLE qtest
FIELDS TERMINATED BY \\"\ \\"
LINES TERMINATED BY \\"\
\\"
(
Test_Date_Received,
Test_Date_Issued,
Test_Issued_wkNo,
Test_QuoteNo,
Test_Status,
Test_New_Name,
Test_Cust_Name,
Test_Cust_Contact,
Test_Consultant,
Test_Nett_Value£,
Test_Nett_Value€,
Test_Number,
Test_Rep,
Test_Specified,
Test_Revision,
Test_Comments
)";
mysql_query($q) or die(mysql_error());
echo "Found a total of $lines records in this csv file.\
";
?>