我尝试了以下代码,但出现了一些错误。在这里我可以读取输入文件,但出现以下错误:Deprecated: Function split() is deprecated in C:\wamp\www\aaj2\index.php on line 63。 O/P:在这个csv文件中一共找到了5124条记录。
<?php
$databasehost = "localhost";
$databasename = "test";
$databasetable = "sample";
$databaseusername="test";
$databasepassword = "";
$fieldseparator = ",";
$lineseparator = "\n";
$csvfile = "filename.csv";
$addauto = 0;
$save = 1;
$outputfile = "output.sql";
if(!file_exists($csvfile)) { echo "File not found. Make sure you specified the correct path.\n"; exit; }
$file = fopen($csvfile,"r");
if(!$file) { echo "Error opening data file.\n"; exit; }
$size = filesize($csvfile);
if(!$size) { echo "File is empty.\n"; 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," \t"); $line = str_replace("\r","",$line); /************************************ This line escapes the special character. remove it if entries are already escaped in the csv file ************************************/ $line = str_replace("'","\'",$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 . "\n";
@mysql_query($query); }
@mysql_close($con);
if($save) { if(!is_writable($outputfile)) { echo "File is not writable, check permissions.\n"; } else { $file2 = fopen($outputfile,"w");
if(!$file2) { echo "Error writing to the output file.\n"; } else { fwrite($file2,$queries); fclose($file2); } } }
echo "Found a total of $lines records in this csv file.\n";
?>
编辑:错误:文件不可写,检查权限。在这个csv文件中一共找到了5124条记录。
最佳答案
几个提示:
Don't use the deprecated ext/mysql ,当你可以使用 ext/mysqli 或 PDO 时。
不要将整个 csv 文件读入 PHP 变量。当文件为 500MB 时会发生什么情况?
当您可以使用内置函数 fgetcsv() 时,不要编写自定义 PHP 代码来解析 csv 数据.
当您可以使用 prepared statements 时,不要为数据中的每一行创建新的 SQL 语句.
不要将外部文件中的数据插入到 SQL 语句中。这有风险 SQL injection漏洞,就像插入不受信任的用户输入时一样。
当你可以使用 MySQL 的 LOAD DATA INFILE 时,不要逐行解析和插入 csv 数据。命令。它比逐行插入快 20 倍。
这里有一个更简单的解决方案:
<?php
$databasehost = "localhost";
$databasename = "test";
$databasetable = "sample";
$databaseusername="test";
$databasepassword = "";
$fieldseparator = ",";
$lineseparator = "\n";
$csvfile = "filename.csv";
if(!file_exists($csvfile)) {
die("File not found. Make sure you specified the correct path.");
}
try {
$pdo = new PDO("mysql:host=$databasehost;dbname=$databasename",
$databaseusername, $databasepassword,
array(
PDO::MYSQL_ATTR_LOCAL_INFILE => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
)
);
} catch (PDOException $e) {
die("database connection failed: ".$e->getMessage());
}
$affectedRows = $pdo->exec("
LOAD DATA LOCAL INFILE ".$pdo->quote($csvfile)." INTO TABLE `$databasetable`
FIELDS TERMINATED BY ".$pdo->quote($fieldseparator)."
LINES TERMINATED BY ".$pdo->quote($lineseparator));
echo "Loaded a total of $affectedRows records from this csv file.\n";
?>
我在 Mac 上使用 PHP 5.3.26 测试了这个,连接到 Linux 上的 MySQL 5.6.14。
关于将csv数据导入mysql的PHP脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20876043/