PHP CSV-MYSQL超时
首先,我搜索了相关的答案,有一些-但找不到任何非常具体的我要找的。我相信有更好的方法 基本上,我在寻找一些建议,我有一个脚本,其中正在将服务器上的CSV文件转换为MYSQL数据库。但是,CSV文件是20mb,脚本刚刚超时。我已经修改了php.ini,所以它不应该超时,但它会导致500个内部服务器错误。有没有一种方法可以保持页面处于活动状态而不超时(jquery会这样做吗)-如果没有,我知道我过去曾对大型SQL文件使用过类似shell的执行,有没有其他方法可以使用这种方法?我非常感谢你的建议。我的编程知识不是很好,所以请耐心等待 以下是我正在使用的脚本:PHP CSV-MYSQL超时,php,mysql,csv,import,Php,Mysql,Csv,Import,首先,我搜索了相关的答案,有一些-但找不到任何非常具体的我要找的。我相信有更好的方法 基本上,我在寻找一些建议,我有一个脚本,其中正在将服务器上的CSV文件转换为MYSQL数据库。但是,CSV文件是20mb,脚本刚刚超时。我已经修改了php.ini,所以它不应该超时,但它会导致500个内部服务器错误。有没有一种方法可以保持页面处于活动状态而不超时(jquery会这样做吗)-如果没有,我知道我过去曾对大型SQL文件使用过类似shell的执行,有没有其他方法可以使用这种方法?我非常感谢你的建议。我的
<?php
$databasehost = "localhost";
$databasename = "test";
$databasetable = "sample";
$databaseusername ="test";
$databasepassword = "";
$fieldseparator = ",";
$lineseparator = "\n";
$csvfile = "filename.csv";
/********************************/
/* Would you like to add an ampty field at the beginning of these records?
/* This is useful if you have a table with the first field being an auto_increment integer
/* and the csv file does not have such as empty field before the records.
/* Set 1 for yes and 0 for no. ATTENTION: don't set to 1 if you are not sure.
/* This can dump data in the wrong fields if this extra field does not exist in the table
/********************************/
$addauto = 0;
/********************************/
/* Would you like to save the mysql queries in a file? If yes set $save to 1.
/* Permission on the file should be set to 777. Either upload a sample file through ftp and
/* change the permissions, or execute at the prompt: touch output.sql && chmod 777 output.sql
/********************************/
$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不同,您应该能够在一个MySQL命令中使用。这将把处理转移到MySQL,这意味着PHP不会超时。如果仍然超时,您可以始终从shell运行相同的命令。不要解析您自己的CSV
使用PHP的内置函数了解如何使用fgetcsv()等函数请原谅我的知识不足,但加载数据填充是否可以用PHP编写?@bushell当然,这只是一个MySQL命令,您应该能够使用任何PHP MySQL库在数据库服务器上运行它。非常感谢,根据你的结果进行了一点搜索,我完成了工作。如果其他人想知道:$query=“加载数据本地填充”aw2\u dap\u combined\u all\u csv.csv“到以“,”结尾的表移动字段中,“\n”结尾的行”;