导入CSV时php上的未定义偏移量错误 [英] Undefined offset error on php when importing a CSV
问题描述
我正在尝试将CSV文件导入到我的数据库中,并且最后成功执行了该文件.但是,在执行过程中,我收到一条未定义的偏移量"错误消息,当我检查导入的数据时,我发现表中有一些空记录已更新.如何避免将这些空单元格导入数据库?我也不想看到这些错误消息.
I am trying to import a CSV file into my database and it is being successfully executed at the end. But, during the execution, I receive an "undefined offset" error message and when I checked the data imported, I see that there are some null records updated in the table. How can I avert importing these null cells into my database? I also would like not to see these error messages.
<?php
require_once("database.php");
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
/// Delete table contents
$dsql = "TRUNCATE TABLE User_Mirror_Tbl";
if ($conn->query($dsql) === TRUE) {
echo "Table content is truncated successfully". PHP_EOL;
} else {
echo "Error: " . $dsql . "<br>" . $conn->error;
}
//read file
$csvfile=file_get_contents("/samba/import/User_Update_Tbl.csv");
//counters:
$record_number=0;
$record_number_err=0;
$lines = explode(PHP_EOL, $csvfile);
$array = array();
foreach ($lines as $line) {
$field = str_getcsv($line);
if $field[0] != ''){
$field[1]= ( $field[1] == '' ? NULL : $field[1]);
$field[6]= ( $field[6] == '' ? NULL : $field[6]);
$field[7]= ( $field[7] == '' ? NULL : $field[7]);
$sql="INSERT INTO User_Mirror_Tbl (History_Record_ID, Employee_ID, Application_ID, User_Status, Record_Date, User_Name, User_Role, Last_Signon, UserKeyString)
VALUES
('$field[0]','$field[1]','$field[2]','$field[3]','$field[4]','$field[5]','$field[6]','$field[7]','$field[8]') ";
//insert record to database
if ($conn->query($sql) === TRUE) {
// echo "New record created successfully". PHP_EOL;
$record_number=$record_number+1;
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
$record_number_err=$record_number_err+1;
}
}
}
echo $record_number.' Successful record and '.$record_number_err.' Unsuccessful record executed.';
$conn->close();
<?php
require_once("database.php");
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
/// Delete table contents
$dsql = "TRUNCATE TABLE User_Mirror_Tbl";
if ($conn->query($dsql) === TRUE) {
echo "Table content is truncated successfully". PHP_EOL;
} else {
echo "Error: " . $dsql . "<br>" . $conn->error;
}
//read file
$csvfile=file_get_contents("/samba/import/User_Update_Tbl.csv");
//counters:
$record_number=0;
$record_number_err=0;
$lines = explode(PHP_EOL, $csvfile);
$array = array();
foreach ($lines as $line) {
$field = str_getcsv($line);
if $field[0] != ''){
$field[1]= ( $field[1] == '' ? NULL : $field[1]);
$field[6]= ( $field[6] == '' ? NULL : $field[6]);
$field[7]= ( $field[7] == '' ? NULL : $field[7]);
$sql="INSERT INTO User_Mirror_Tbl (History_Record_ID, Employee_ID, Application_ID, User_Status, Record_Date, User_Name, User_Role, Last_Signon, UserKeyString)
VALUES
('$field[0]','$field[1]','$field[2]','$field[3]','$field[4]','$field[5]','$field[6]','$field[7]','$field[8]') ";
//insert record to database
if ($conn->query($sql) === TRUE) {
// echo "New record created successfully". PHP_EOL;
$record_number=$record_number+1;
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
$record_number_err=$record_number_err+1;
}
}
}
echo $record_number.' Successful record and '.$record_number_err.' Unsuccessful record executed.';
$conn->close();
推荐答案
有时您需要忽略csv的最后一行.我将顶部忽略的行数设置为$ start_offset,将底部忽略的行数设置为$ end_offset.从零开始,然后增加直到偏移误差消失
这是我的方法:
Sometimes you need to ignore the last line or lines of the csv. I set the number of ignore lines from the top as $start_offset and the number of lines to ignore from the bottom as $end_offset. Start with zero and increase until the offset error goes away
Here's how I do it:
$data = file_get_contents($filename);//load up csv
$data_array = explode("\n", $data);//break file into lines
$csv = array_map('str_getcsv', $data_array);//break up comma delimited
$csv_len = count($csv); //count of number of lines
$start_offset = 2;
$end_offset = 3;
for ($i=$start_offset; $i<$csv_len-$end_offset; $i++)
{
//access columns as $csv[$i][0], $csv[$i][1] etc
}
这篇关于导入CSV时php上的未定义偏移量错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!