请不要关闭这个问题,我知道已经购买了很多解决方案,它们似乎都不起作用,或者至少对我不起作用。我有来自 google 联盟网络产品提要的 csv 文件,它采用 zip、.txt (csv) 制表符分隔符。到目前为止,我已经解压缩文件并读取文件,但我想做的是将该数据转储到 mysql 数据库,csv 的第一行是标题,我想根据 csv 文件创建字段名称并将数据分别放入数据库中,一些csv 文件中的字段为空。我还希望能够再次运行它(可能我会为此运行 cron 作业)并只在其中插入新数据。我的 csv 文件示例:
ProductID ProductName ProductURL BuyURL ImageURL Category CategoryID PFXCategory BriefDesc ShortDesc IntermDesc LongDesc ProductKeyword Brand Manufacturer ManfID ManufacturerModel UPC Platform MediaTypeDesc MerchandiseType Price SalePrice VariableCommission SubFeedID InStock Inventory RemoveDate RewPoints PartnerSpecific ShipAvail ShipCost ShippingIsAbsolut ShippingWeight ShipNeeds ShipPromoText ProductPromoText DailySpecialsInd GiftBoxing GiftWrapping GiftMessaging ProductContainerName CrossSellRef AltImagePrompt AltImageURL AgeRangeMin AgeRangeMax ISBN Title Publisher Author Genre Media Material PermuColor PermuSize PermuWeight PermuItemPrice PermuSalePrice PermuInventorySta Permutation PermutationSKU BaseProductID Option1 Option2 Option3 Option4 Option5 Option6 Option7 Option8 Option9 Option10 Option11 Option12 Option13 Option14 Option15 Option16 Option17 Option18 Option19 Option20
4181615950845mkTWIN~TWIN Brylanehome Jasmine Quilt Set (Sea Green,Twin) http://gan.doubleclick.net/gan_click?lid=41000613802463546&pid=4181615950845mkTWIN~TWIN&adurl=http%3A%2F%2Fwww.brylanehome.com%2FProduct.aspx%3FPfId%3D20653%26ProductTypeId%3D2%26affiliate_id%3D017%26mr%3AtrackingCode%3D8C875316-BB51-E211-9A4A-90E2BA0278A8%26mr%3AreferralID%3DNA&usg=AFHzDLtiRj_wSyAQFEPxMBFVo4HjTeHMVA&pubid=21000000000568460 http://gan.doubleclick.net/gan_click?lid=41000613802463546&pid=4181615950845mkTWIN~TWIN&adurl=http%3A%2F%2Fwww.brylanehome.com%2FProduct.aspx%3FPfId%3D20653%26ProductTypeId%3D2%26affiliate_id%3D017%26mr%3AtrackingCode%3D8C875316-BB51-E211-9A4A-90E2BA0278A8%26mr%3AreferralID%3DNA&usg=AFHzDLtiRj_wSyAQFEPxMBFVo4HjTeHMVA&pubid=21000000000568460 http://media.redcatsecom.com/brylanehome/mc/1595_41816_mc_0845.jpg?wid=230&hei=331&qlt=95&op_sharpen=1 Bedding > Quilts Picture perfect on any bed, this pieced and stitched quilt set is the ideal choice for spring bedding.   • A BrylaneHome® Exclusive!  • all-over floral print quilt reverses to a stripe • scalloped edges • 100% cotton Picture perfect on any bed, this pieced and stitched quilt set is the ideal choice for spring bedding.   • A BrylaneHome® Exclusive!  • all-over floral print quilt reverses to a stripe • scalloped edges • 100% cotton face/cotton/poly fill • available in 3 sizes: Twin 2-Pc. Set 68" x 90", Full/Queen 3-Pc. 86" x 86", and King 3-Pc. 100" x 90" • machine wash/dry • imported • pair this quilt with any of our colorful and elegant sheets to create a dynamic layered look • and shop our selection of total bed sets to reinvent your décor overnight   This vibrant quilt set includes: 1 quilt and 2 standard shams (1 with twin).    Why Buy?  Our customers agree that our patchwork bedding shows off their vibrant and fun décors while offering them incredible comfort at great values.  BrylaneHome Brylane Home 4181615950845mkTWIN~TWIN new 114.99 59.99 in stock 47 adult Brylanehome Jasmine Quilt Set (Sea Green,Twin) SEA GREEN TWIN Home & Garden > Linens & Bedding > Bedding > Quilts & Quilt Sets 4181615950845mkTWIN~TWIN 1595-41816
我用来解压的脚本:
<?php
$zip = new ZipArchive;
$res = $zip->open('K9349.zip');
if ($res === TRUE) {
$zip->extractTo('extracted/');
$zip->close();
echo 'Unzip was successful';
} else {
echo 'Unzip was not successful';
}
?>
以下是我尝试过的两个脚本,但没有任何运气。脚本1:
<?php
$row = 1;
if (($handle = fopen("extracted/K9349.txt", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
?>
脚本 2:
<?php
$host = '****';
$user = '****';
$pass = '****';
$database = '****';
$db = mysql_connect($host, $user, $pass);
mysql_query("use $database", $db);
/********************************************************************************/
// Parameters: filename.csv table_name
$file = 'extracted/K9349.txt';
$argv = $_SERVER[argv];
if($argv[1]) { $file = $argv[1]; }
else {
echo "Please provide a file name\n"; exit;
}
if($argv[2]) { $table = $argv[2]; }
else {
$table = pathinfo($file);
$table = $table['filename'];
}
/********************************************************************************/
// Get the first row to create the column headings
$fp = fopen($file, 'r');
$frow = fgetcsv($fp);
$ccount = 0;
foreach($frow as $column) {
$ccount++;
if($columns) $columns .= ', ';
$columns .= "$column varchar(250)";
}
$create = "create table if not exists $table ($columns);";
mysql_query($create, $db);
/********************************************************************************/
// Import the data into the newly created table.
$file = $_SERVER['PWD'].'/'.$file;
$q = "load data infile '$file' into table $table fields terminated by ',' ignore 1 lines";
mysql_query($q, $db);
?>
如果您能提供帮助,我将不胜感激。