There is a well-known from the network script import csv in the database Mysql. Everything works as it should, with one big and fat drawback that covers all the advantages. You need to import about 100 thousand lines. Everything works terribly long and for the most part does not end with success. Tell me, how is it better to rewrite the script so that it works more steadily? It is also possible to use PDO, but I am not strong in it.
if ($_FILES[csv][size] > 0) { //Получаем CSV файл $file = $_FILES[csv][tmp_name]; $handle = fopen($file,"r"); //Обрабатываем в цикле CSV файл и добавляем данные в БД do { if ($data[0]) { mysql_query("INSERT INTO price_minsk (brand, artikul, description, ostatok, kratnost, valuta, price) VALUES ( '".addslashes($data[0])."', '".addslashes($data[1])."', '".addslashes($data[2])."', '".addslashes($data[3])."', '".addslashes($data[4])."', '".addslashes($data[5])."', '".addslashes($data[6])."' ) "); } } while ($data = fgetcsv($handle,1000,";","'")); // //redirect header('Location: import.php?success=1'); die; } Also found such a function, which, as the manuals say, works quickly. But how to attach it to the above script?
function import_csv( $table, // Имя таблицы для импорта $afields, // Массив строк - имен полей таблицы $filename, // Имя CSV файла, откуда берется информация // (путь от корня web-сервера) $delim=',', // Разделитель полей в CSV файле $enclosed='"', // Кавычки для содержимого полей $escaped='\\', // Ставится перед специальными символами $lineend='\\r\\n', // Чем заканчивается строка в файле CSV $hasheader=FALSE){ // Пропускать ли заголовок CSV if($hasheader) $ignore = "IGNORE 1 LINES "; else $ignore = ""; $q_import = "LOAD DATA INFILE '". $_SERVER['DOCUMENT_ROOT'].$filename."' INTO TABLE ".$table." ". "FIELDS TERMINATED BY '".$delim."' ENCLOSED BY '".$enclosed."' ". " ESCAPED BY '".$escaped."' ". "LINES TERMINATED BY '".$lineend."' ". $ignore. "(".implode(',', $afields).")" ; return mysql_query($q_import); }
addslashes()to escape request parameters is incorrect - tutankhamun