この質問を閉じないでください。すでに多くのソリューションが購入されていますが、どれも機能していないように見えるか、少なくとも私にとっては機能していません。Google アフィリエイト ネットワークの製品フィードから csv ファイルを取得しました。zip、.txt (csv) タブ区切りで提供されます。これまでファイルを解凍してファイルを読みましたが、やりたいことはそのデータをmysqlデータベースにダンプすることです.csvの1行目はヘッダーです.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';
}
?>
以下は、私が試した 2 つのスクリプトです。スクリプト 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);
?>
お役に立てれば本当に助かります。