2012-04-04 8 views
-1

ようこそ!カンマ区切りのテキストファイル.csvをmysqlデータベースフィールドに変換するスクリプトを作成するにはどうすればよいですか?

Ok ... .csvファイルの内容をmySQLデータベースの単一のテーブルにインポートするスクリプトを作成しようとしています。 .csvファイル内の各列/ヘッダーに対して、そのテーブル内にフィールドを作成するスクリプトを作成したいと思います。

私の.csvデータは、この(私は最初の2行を含む)のようになります。

<?php 
    "PROGRAMNAME","PROGRAMURL","CATALOGNAME","LASTUPDATED","NAME","KEYWORDS","DESCRIPTION","SKU","MANUFACTURER","MANUFACTURERID","UPC","ISBN","CURRENCY","SALEPRICE","PRICE","RETAILPRICE","FROMPRICE","BUYURL","IMPRESSIONURL","IMAGEURL","ADVERTISERCATEGORY","THIRDPARTYID","THIRDPARTYCATEGORY","AUTHOR","ARTIST","TITLE","PUBLISHER","LABEL","FORMAT","SPECIAL","GIFT","PROMOTIONALTEXT","STARTDATE","ENDDATE","OFFLINE","ONLINE","INSTOCK","CONDITION","WARRANTY","STANDARDSHIPPINGCOST" 
    "TeesforAll.com & WearYourBeer.com","http://www.teesforall.com, http://www.wearyourbeer.com","Product Catalog","10/01/2012","Karate Kid Cobra Kai All Valley '84 Gold Graphic T-Shirt","Karate Kid","Officially licensed Karate Kid Cobra Kai All Valley '84 Gold T-Shirt. Features the Cobra Kai circle logo with '84 on the front. 100% cotton.","ey_84_Gold_Graphic_TShirt-p-2778.html","Karate Kid","","","","USD","","12.99","","","http://www.jdoqocy.com/click-4110789-10569779?url=http%3A%2F%2Fwww.teesforall.com%2FKarate_Kid_Cobra_Kai_All_Valley_84_Gold_Graphic_TShirt-p-2778.html","http://www.ftjcfx.com/image-4110789-10569779","http://www.teesforall.com/images/Karate_Kid_Cobra_Kai_Gold_Shirt.jpg","Mens - Shirts","","","","","","","","","","","","","","","","YES","","","" 

だから私は、私はこのようなものを使用と考えました:$データ出力の

$source_file = "catalogs/tees4all.txt"; 
$handle = fopen("catalogs/tees4all.txt", "r"); 

while (($data = fgetcsv($handle)) !== FALSE) { 
    var_dump($data); 
} 

のvar_dumpを基本的には各列配列としてTHIS ...:

array(40) { [0]=> string(11) "PROGRAMNAME" [1]=> string(10) "PROGRAMURL" [2]=> string(11) "CATALOGNAME" [3]=> string(11) "LASTUPDATED" [4]=> string(4) "NAME" [5]=> string(8) "KEYWORDS" [6]=> string(11) "DESCRIPTION" [7]=> string(3) "SKU" [8]=> string(12) "MANUFACTURER" [9]=> string(14) "MANUFACTURERID" [10]=> string(3) "UPC" [11]=> string(4) "ISBN" [12]=> string(8) "CURRENCY" [13]=> string(9) "SALEPRICE" [14]=> string(5) "PRICE" [15]=> string(11) "RETAILPRICE" [16]=> string(9) "FROMPRICE" [17]=> string(6) "BUYURL" [18]=> string(13) "IMPRESSIONURL" [19]=> string(8) "IMAGEURL" [20]=> string(18) "ADVERTISERCATEGORY" [21]=> string(12) "THIRDPARTYID" [22]=> string(18) "THIRDPARTYCATEGORY" [23]=> string(6) "AUTHOR" [24]=> string(6) "ARTIST" [25]=> string(5) "TITLE" [26]=> string(9) "PUBLISHER" [27]=> string(5) "LABEL" [28]=> string(6) "FORMAT" [29]=> string(7) "SPECIAL" [30]=> string(4) "GIFT" [31]=> string(15) "PROMOTIONALTEXT" [32]=> string(9) "STARTDATE" [33]=> string(7) "ENDDATE" [34]=> string(7) "OFFLINE" [35]=> string(6) "ONLINE" [36]=> string(7) "INSTOCK" [37]=> string(9) "CONDITION" [38]=> string(8) "WARRANTY" [39]=> string(20) "STANDARDSHIPPINGCOST" } 


array(40) { [0]=> string(33) "TeesforAll.com & WearYourBeer.com" [1]=> string(54) "http://www.teesforall.com, http://www.wearyourbeer.com" [2]=> string(15) "Product Catalog" [3]=> string(10) "10/01/2012" [4]=> string(56) "Karate Kid Cobra Kai All Valley '84 Gold Graphic T-Shirt" [5]=> string(10) "Karate Kid" [6]=> string(142) "Officially licensed Karate Kid Cobra Kai All Valley '84 Gold T-Shirt. Features the Cobra Kai circle logo with '84 on the front. 100% cotton." [7]=> string(37) "ey_84_Gold_Graphic_TShirt-p-2778.html" [8]=> string(10) "Karate Kid" [9]=> string(0) "" [10]=> string(0) "" [11]=> string(0) "" [12]=> string(3) "USD" [13]=> string(0) "" [14]=> string(5) "12.99" [15]=> string(0) "" [16]=> string(0) "" [17]=> string(150) "http://www.jdoqocy.com/click-4110789-10569779?url=http%3A%2F%2Fwww.teesforall.com%2FKarate_Kid_Cobra_Kai_All_Valley_84_Gold_Graphic_TShirt-p-2778.html" [18]=> string(44) "http://www.ftjcfx.com/image-4110789-10569779" [19]=> string(68) "http://www.teesforall.com/images/Karate_Kid_Cobra_Kai_Gold_Shirt.jpg" [20]=> string(13) "Mens - Shirts" [21]=> string(0) "" [22]=> string(0) "" [23]=> string(0) "" [24]=> string(0) "" [25]=> string(0) "" [26]=> string(0) "" [27]=> string(0) "" [28]=> string(0) "" [29]=> string(0) "" [30]=> string(0) "" [31]=> string(0) "" [32]=> string(0) "" [33]=> string(0) "" [34]=> string(0) "" [35]=> string(0) "" [36]=> string(3) "YES" [37]=> string(0) "" [38]=> string(0) "" [39]=> string(0) "" } 

私は誰かがに$データから取得する私を助けることができることを期待しています列と行が一致するデータベーステーブル... CSVのヘッダーと行!

ありがとうございます!

+1

http://dev.mysql.com/doc/refman/5.1/en/load-data.htmlどのように書式設定について。 –

+0

LOAD DATA INFILE:http://dev.mysql.com/doc/refman/5.1/en/load-data.html –

+0

これは[phpMyAdmin]と呼ばれています(http://www.phpmyadmin.net/home_page/index.php ) – hohner

答えて

1

これを試してみてください。

<? 

$source_file = "catalogs/tees4all.txt"; 
$handle = fopen("catalogs/tees4all.txt", "r"); 

$col_names = implode(",", fgetcsv($handle)); // Getting comma separated list of col names 

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password'); 
mysql_select_db("yourdbhere"); 
while (($data = fgetcsv($handle)) !== FALSE) { 
    $values = ""; 
    foreach($data as $key => $value) { 
     if ($key != 0) $values .= ", "; 
     $values .= "'".mysql_escape_string($value)."'"; 
    } 
    mysql_query('INSERT INTO yourtablehere ('.$col_names.') VALUES ('.$values.')'); 
} 

それとも使用

+0

お返事ありがとうございました...私はあなたの提案をともにして遊んでいます...しかし、私はあまり運がありませんでした: -/ 私は白いページを取得しています...間違いなく....そしてデータがDBに入ることはありません...しかし...私はjoomlaで働いていて、物事は少し "異なっています" そう...私は本当に私がそれを理解することを望んだあなたが示唆したようなSQL ...私はちょうどデータベースにデータを取得しようとしています... そ...私はこれと正確に何をしますか? phpmyadminで実行しますか?また、変数が何であるべきかを説明することもできます。 :) – DigitalMediaGuy

+0

LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name' [置換え| IGNORE] INTO TABLE tbl_name [キャラクタセット文字セット名] [{フィールド| [ 'ストリング' BY TERMINATED] COLUMNS} [ '列' BY TERMINATED] [OPTIONALLY] '文字' BY ENCLOSED] [ '文字' BY ESCAPED] ] [LINES [ '列' により最初] ] [(col_name_or_user_var、...)] [SET col_name = expr、...] – DigitalMediaGuy

+0

cvsファイルの列名は、MySQLの列名と一致する必要があります表。また、更新された例を見てください。 "$値。" '$値'にする必要があります。 –

関連する問題