如何解决将CSV导入SQL表可在XAMPP上使用,但不能在在线服务器上使用
我在PHP中创建了一个函数,可以将文件夹中存在的一堆csv文件中的数据写入SQL表中。
该函数在我的PC上的XAMPP服务器上可以正常运行,它创建表并写入从数十个CSV文件中提取的数千行,但是当我在在线服务器上导出该函数时,表已正确创建,正确打开了CSV文件(我已通过print_r对其进行了检查),但是CSV数据未写入和SQL表中,并且未显示任何错误消息。
这是我的代码:
<?PHP
include('sys/dbconnect.php');
$sql1 = "CREATE TABLE IF NOT EXISTS
milk
(
milk_record int(10) NOT NULL,panel_number int(3) NOT NULL,...etc.... (41 columns in total)
PRIMARY KEY (milk_record)
)";
if($conn->query($sql1) === TRUE) {echo "Table ready <br>";}
else { echo "Error creating database: " . $conn->error . "<br>";}
$conn->close();
$dir = 'csvfiles/';
$files = scandir($dir);
$fileCount = 0;
foreach($files as $fn)
{
if($fileCount < 2) { $fileCount++; continue;}
$filename=$dir.$fn;
if ($fn)
{
$directory = 'csvfiles/';
// echo "- file: ".$directory.$fn." <br> ";
include('sys/dbconnect.php');
ini_set('auto_detect_line_endings',TRUE);
$file = fopen($filename,"r");
set_time_limit (240);
while ($getData = fgetcsv($file,','"'))
{
// print_r($getData);
$sql = "INSERT INTO milk
(
milk_record,panel_number,....etc.... (41 columns in total)
)
VALUES (?,?,?) ";
$stmt = mysqli_prepare($conn,$sql);
$stmt->bind_param
( "sssssssssssssssssssssssssssssssssssssssss",$getData[0],$getData[1],$getData[2],$getData[3],$getData[4],$getData[5],$getData[6],$getData[7],$getData[8],$getData[9],$getData[10],$getData[11],$getData[12],$getData[13],$getData[14],$getData[15],$getData[16],$getData[17],$getData[18],$getData[19],$getData[20],$getData[21],$getData[22],$getData[23],$getData[24],$getData[25],$getData[26],$getData[27],$getData[28],$getData[29],$getData[30],$getData[31],$getData[32],$getData[33],$getData[34],$getData[35],$getData[36],$getData[37],$getData[38],$getData[39],$getData[40]
);
$sql = "INSERT into milk() values(?,?)";
$stmt->execute();
}
fclose($file);
$delete_first_line = "DELETE FROM milk WHERE milk_record =0;";
$delete = mysqli_query($conn,$delete_first_line);
mysqli_close($conn);
?>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。