我正在使用下面的 PHP 代码批量插入 SQL Server 中的数据库表。我已经有一个名为ShopifyItem
(有 25 列)的表,但是当我使用此代码时,我得到了
未定义的偏移量:24
错误消息,在Item
表中我只看到(正确插入了 26 行),即使 csv 文件的行数也超过 1,000。
谁能告诉我怎么了?
PHP代码:
$server = "**\**,1433";
$connectionInfo = array( "Database"=>"**", "UID"=>"**", "PWD"=>"**" );
$conn = sqlsrv_connect( $server, $connectionInfo );
if( $conn === false ) {
die( print_r( sqlsrv_errors(), true));
}
$file_handle = fopen("Table.csv", "r");
while (!feof($file_handle) ) {
$line_of_text = fgetcsv($file_handle, 1024);
$tsql = "INSERT INTO Item (Handle, Title, [Body(HTML)], Vendor, [Type], Tags, [Option1 Name],[Option1 Value], [Option2 Name], [Option2 Value], [Option3 Name], [Option3 Value], [Variant SKU], [Variant Grams], [Variant Inventory Tracker],[Variant Inventory Qty], [Variant Inventory Policy], [Variant Fulfillment Service], [Variant Price], [Variant Compare At Price], [Variant Requires Shipping], [Variant Taxable], [Image Src], HQID, WebPrice)
VALUES ('$line_of_text[0]','$line_of_text[1]', '$line_of_text[2]', '$line_of_text[3]', '$line_of_text[4]', '$line_of_text[5]', '$line_of_text[6]', '$line_of_text[7]', '$line_of_text[8]', '$line_of_text[9]', '$line_of_text[10]', '$line_of_text[11]', '$line_of_text[12]', '$line_of_text[13]', '$line_of_text[14]', '$line_of_text[15]', '$line_of_text[16]', '$line_of_text[17]', '$line_of_text[18]', '$line_of_text[19]', '$line_of_text[20]', '$line_of_text[21]', '$line_of_text[22]', '$line_of_text[23]', '$line_of_text[24]' )";
if( sqlsrv_query( $conn, $tsql))
{
echo "Statement executed.\n";
}
else
{
echo "Error in statement execution.\n";
die( print_r( sqlsrv_errors(), true));
}
}