Last active
August 29, 2015 14:10
-
-
Save Stoffo/cd0083fd7698a70dcb1e to your computer and use it in GitHub Desktop.
import a CSV file easily with this file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php | |
<?php | |
error_reporting(0); | |
$server = "localhost"; | |
$port = 3306; | |
$username = "csv_test"; | |
$password = "test"; | |
$db = "test_db"; | |
$table_name = "test"; | |
//-------------------------------------------------------------------- | |
$admin_address = "[email protected]"; | |
$from_name = "CSV Importer"; | |
$from_mail = "[email protected]"; | |
// Wenn true, werden auch Mails bei Success geschickt: | |
$mail = true; | |
//-------------------------------------------------------------------- | |
$error = null; | |
$time_start = microtime(true); | |
$mysqli = new mysqli($server, $username, $password, $db, $port); | |
if ($mysqli->ping()) { | |
$mysqli->set_charset("utf8"); | |
if (!$mysqli->query("TRUNCATE `{$table_name}`;")) { | |
$error .= "Error while doing Query:\n\n".$mysqli->error."\n"; | |
} | |
//Daten direkt aus der CSV in die DB schreiben: | |
//@ignore bedeutet, dass eine Spalte der CSV übersprungen wird. | |
if (!$mysqli->query(" | |
LOAD DATA LOCAL INFILE '/root/bin/tmp/tchibo.csv' | |
INTO TABLE `$table_name` | |
FIELDS TERMINATED BY ';' | |
OPTIONALLY ENCLOSED BY '\"' | |
LINES TERMINATED BY '\r\n' | |
IGNORE 1 LINES | |
(@ignore, | |
title, | |
@ignore, | |
@price_csv, | |
@ignore, | |
@deeplink_csv, | |
@ignore, | |
category, | |
@ignore, | |
@ignore, | |
img_url, | |
@ignore, | |
@week_csv, | |
@ignore, | |
@ignore, | |
@ignore, | |
@ignore, | |
@ignore) | |
SET | |
price = REPLACE(@price_csv, 'EUR',''), | |
deeplink = SUBSTRING(@deeplink_csv, 249), | |
start_week = REPLACE(@week_csv, 'KW',''), | |
start_week = SUBSTRING_INDEX(start_week, '/', 1) | |
") | |
) { | |
$error .= "Error while doing Query:\n\n".$mysqli->error."\n"; | |
} | |
} else { | |
$error = "Error: ".$mysqli->error." (Server down?)"; | |
} | |
$time_end = microtime(true); | |
$exec_time = round($time_end - $time_start, 2)." Seconds"; | |
$date = date("d.m.Y - H:i:s", time()); | |
// Mail-Versand bei Fehler: | |
if ($error != null) { | |
$mail_topic = "Error while processing csv file"; | |
$mail_text = wordwrap($error."\n\n Duration of Execution: ".$exec_time."\n\n".$date); | |
$param = "From: $from_name <$from_mail>\n"; | |
mail($admin_address, $mail_topic, $mail_text, $param); | |
exit(0); | |
} // Mail-Versand bei Erfolg | |
elseif ($mail == true && $error == null) { | |
$mail_topic = "Successfully imported csv file"; | |
$mail_text = wordwrap("Duration of Execution: ".$exec_time."\n\n".$date); | |
$param = "From: $from_name <$from_mail>\n"; | |
mail($admin_address, $mail_topic, $mail_text, $param); | |
exit(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment