PHPスクリプトは、MySQLにCSVデータをインポートする

これは、あなたのデータベースにCSVデータをインポートできるようにする簡単なスクリプトです。 あなたは、単に適切なフィールドを編集することができますので、csvファイルと一緒にアップロードして、Webサイトからそれを呼び出して、それが残りの部分を行いますこれは便利だ。

それは昏睡状態であるかどうか、このcsvファイルの区切り文字を指定することができます、タブなどがこれまた、行区切り文字を選択することができ、あなたはファイル(データをSQLダンプとも呼ばれる)に出力を保存することができます。

また、通常、主キーの自動インクリメントの整数であり、各行の先頭に空のフィールドを含めることができます。

あなたはphpmyadminの持っていない主場合、またはには、ログインの手間を必要としない、数回のクリックのソリューションを望んでいるこのスクリプトは有用であるか、単にコマンドプロンプトで男です。
ちょうどテーブルが既にデータをダンプする前に作成されていることを確認します。
あなたがバグレポートを得た場合、親切にご意見を投稿してください。

ファイルのダウンロードはこちら

タグ:
  1. 作業および解析のCSVテキストファイルとPHP
    17:52 2011年5月15日
  2. Joln
    07:52 2011年5月11日

    素晴らしい脚本! tnxにTNXたくさん

  3. CNAのクラステキサス
    07:10 2011年5月11日

    私がこの記事を...本当に私はこのポストから感銘を受けて.. ...感謝して任意の単語を持っていない。それは、これを共有するための素晴らしい人間..おかげされたこの記事を作成する人

    私たち。

  4. CNAのクラスハワイ
    07:07 2011年5月11日

    ああ...有益なポストは本当にthatsの。 私は新しいブロガーにそれは私のために必ず役立つ情報になるよ。 共有をありがとう!

  5. フェザーオウル
    12:19 2011年5月8日

    スクリプトは、かなりのとおりです私にとっては素晴らしい作品。 感謝します!

  6. umair
    07:39 2011年5月2日

    こんにちは...
    私は問題に直面して、私はこれは、そのエラーを言って表示を使用する場合
    "非推奨:機能分割()C言語で推奨されていません:\ WAMPを\ wwwに\をtest1 \ simplecsvimport.phpを64行で
    ファイルが書き込み可能ではありませんが、アクセス許可を確認してください。 見つかりませんこのcsvファイル41のレコードの合計。 "

    Plsの回答は、間違っているんです!
    事前に感謝!

  7. justkid
    10:25 2011年4月7日

    私は本当にこのコードで混乱しています!
    しかし、私は試していただきたい!
    ところで、コードまたはフィールドのHTMLは、このコードを呼び出す方法

  8. して、Jorge Diasさん
    19:01 2011年3月14日

    おかげで、非常に良いcsvファイルのインポーターです。
    しかし、私は一つの小さな問題を発見、それだけで構造とcsvファイルで動作します:"フィールド1"は、"フィールド2"は、"フィールド3"などが、私はこのようなCSVファイルをhava:フィールド1;フィールド2; filed3;等

    私は変更することができる唯一の​​ことはja項目$であるどのようにファイルが(フィールド区切り)"を使用していないことをスクリプトを'伝える'ことができますか?
    マイideia、または$を持つコンボボックスja項目(された ;) 別の$ 1つをfielddelimiter("またはなし)

    事前に感謝します。

  9. ラスティー
    Febは夜02時56分2011で、3

    それらのコンマでフィールドが新しいエントリとして読んで取得しないように二重引用符で囲まれているフィールドのために働くために、このスクリプトを取得する方法のいずれかのアイデア?

    私は、ドル金額は、ユーザーは、カンマを使用している可能性がありますコメント欄のようにコンマが含まれているデータを持っている。 私は、インポート、フィールド全体を必要とする、このスクリプトは、新しいフィールドとして扱います。

  10. qwewe
    14時56時2011年1月23日

    マーククロイド@
    "アドレス=の値(アドレス)
    '='予期しない構文エラー:私はパースエラーとしてここでエラーを取得する

  11. ronbowalker
    17:54 2011年1月15日

    @ joofoo
    私はあなたのコードが読み込まれているが、私はMySQLデータベースには更新情報を取得...

    CSVファイルが含まれています:
    ヘッダ - >ユーザ名、パスワード、メッセージ、電子メール、画像、日
    Row2 - >ビル、男、、、、
    Row3 - >ジェーン、ギャル、、、、
    Row4 - > Davidさん、子供、、、、

    そして、ここにコードされ...:
    する<?PHPの

    / /"リモート"フォルダポップアップiphonelogin.phpファイルからの設定...
    / *は、dbに接続します* /
    / /する$ link = mysql_connect('ronbo.db.6405862.hostedresource.com'、'ronbo'、'Pepper0689')または死ぬ('DBに接続できません');
    / / mysql_select_db('ronbo'、$ link)のか死ぬか('DBを選択することはできません');

    / / https://www.ihappyapps.com/DataFolder/loader.php
    /********************************/
    / *コードでhttps://legend.ws/blog/tips-tricks/csv-php-mysql-import/
    /* Edit the entries below to reflect the appropriate values
    /********************************/
    $ databasehost ="dbhostの";
    $ databasenameは="名前";
    $ databasetable ="テーブル";
    $ databaseusername ="ユーザ名";
    $ databasepassword ="パスワード";
    $ ja項目="";
    $ lineseparator ="\ Ñ";
    $ csvfile ="spreadsheet.csv";
    /********************************/
    $ addauto = 1;
    /********************************/
    $ = 0の保存します。
    $ outputfileに="output.sql";
    /********************************/

    するif(!file_existsは($ csvfile)){
    echo"サーバは、あなたの要求を持って、データベースにロードするCSVファイルが存在して... ...ではありません\ n";
    終了;
    }

    $は、= 1 *(1024 * 1024)チャンク;
    $ファイルの= fopen($ csvfile、"rbを");
    場合(!$ファイル){
    "オープニングデータファイルエラーが発生する\ n"エコー;
    終了;
    }

    $サイズ=ファイルサイズ($ csvfile);

    場合(!$サイズ){
    "ファイルが空です\ n"エコー;
    終了;
    }

    $コン= @($ databasehost、$ databaseusername、$ databasepassword)mysql_connectまたは(はmysql_error())死ぬ;
    @ mysql_select_db($ databasenameは)または死ぬ(はmysql_error());

    $行= 0;
    $クエリ="";
    中に(!feofを($ファイル)){
    $ csvline = fgetsは($ファイル、$チャンク);

    $行+ +;
    ($行<2)続けている場合はtrue。

    $行=($ csvline、"\ t")がトリム;

    $行= str_replace("\ rを","",$ライン);

    /************************************************* ************************************************** *********
    この行は、特殊文字をエスケープします。 エントリがすでにcsvファイルでエスケープされている場合、それを削除する
    ************************************************** ************************************************** ********/
    $行= str_replaceの("'"," \'"、$ライン);
    /************************************************* ************************************************** ********/

    $ linearray =($ ja項目、$ライン)爆発;

    $ linemysql = Implodeは("','",$のlinearray);

    $クエリ="の挿入を$ databasetable値('に$ linemysql ');";

    ($保存)$クエリ.= $を照会します。 "\ Ñ";

    @するmysql_fetch_array($クエリ);
    $行をエコー"\ N"を。
    }
    fcloseの($ファイル);

    $行-= 3;

    @するmysql_close($ con)に;

    > <4

    私は実際に動作するようにこれを取得するいくつかのヘルプを使用することができます...
    ロン

  12. joofoo
    11:16 2011年1月11日

    @ Royさん
    私は大きなファイルを処理するとき、それははるかに少ないメモリを消費するように、スクリプトを修正し、(それは私が100MB、CSVファイル上にそれを使用しようとしたときにPHPのメモリ制限を押してください)​​。 変更点は次のとおりです:


    $chunksize = 1*(1024*1024);
    $file = fopen($csvfile,"rb");
    if(!$file) {
    echo "Error opening data file.\n";
    exit;
    }

    $サイズ=ファイルサイズ($ csvfile);

    場合(!$サイズ){
    "ファイルが空です\ n"エコー;
    終了;
    }

    $コン= @($ databasehost、$ databaseusername、$ databasepassword)mysql_connectまたは(はmysql_error())死ぬ;
    @ mysql_select_db($ databasenameは)または死ぬ(はmysql_error());

    $行= 0;
    $クエリ="";
    中に(!feofを($ファイル)){
    $ csvline = fgetsは($ファイル、$チャンク);

    $行+ +;
    ($行<2)続けている場合はtrue。

    $行=($ csvline、"\ t")がトリム;

    $行= str_replace("\ rを","",$ライン);

    /************************************************* ************************************************** *********
    この行は、特殊文字をエスケープします。 エントリがすでにcsvファイルでエスケープされている場合、それを削除する
    ************************************************** ************************************************** ********/
    $行= str_replaceの("'"," \'"、$ライン);
    /************************************************* ************************************************** ********/

    $ linearray =($ ja項目、$ライン)爆発;

    $ linemysql = Implodeは("','",$のlinearray);

    $クエリ="の挿入を$ databasetable値('に$ linemysql ');";

    ($保存)$クエリ.= $を照会します。 "\ Ñ";

    @するmysql_fetch_array($クエリ);
    $行をエコー"\ N"を。
    }
    fcloseの($ファイル);

    $行-= 3;

    @するmysql_close($ con)に;

    ありがとう、

  13. して、Khaled A
    10:35 2011年1月11日

    @にして、Khaled
    どうすればUTF8をエクスポートすることができますoutput.sqlに秀でる
    おかげで

  14. して、Khaled A
    09:46 2011年1月11日

    私はスクリプトを試してみましたoutput.sqlは奇妙なものを書いであり、
    !????????????test_excel値('主キーを挿入|?質問K5のqは質問K5のqは質問K5のqが...。

    これらのことのように
    どのように私はこの問題を修正することができます。 に何か特別な形式は、エクセルシート?

  15. ウォーレン
    18:57 2010年12月30日

    こんにちはみんなは、ちょうどあなたがすべて私は私が必要とする実際のプログラムの書き込みに誰かを見つけたことを知ってもらいたい。

  16. ウォーレン
    01:31 2010年12月29日

    @ワーレン

    /********************************/
    このファイルには、なるように、彼が必要とするものにこのプログラムを作成するソースサイトおよびその他の情報のコメントに基づいて2010年12月28日以降のウォーレン(ないプログラマ)による多くの試みの変更が含まれています。 これは非常に不完全です。 ヘルプは最も評価されて非常に必要。

    行は、ウェブサイトのURLのデータベース名とパスワードなどの入力ボックスを作成するために追加
    /********************************/

    ようこそ、サーバーユーティリティをアップロードする

    と、各ファイル名以下の拡張子、アップロード、テーブルkeywords_categoriesに反するとして前に一意の番号とし、その番号を1を代入:このプログラムは、あなたのウェブサイト上に新しいMySQLデータベースの2つの空のテーブルを作成し、PCから実行するそのディレクトリから処理、後続の各ファイルのいずれかによって、そのファイルからデータ、および増加する番号の最初のコラムの前に空白の列、テーブル内のカテゴリ名のassigend数は、キーワードがされ、それに属しているものと一致するように次に、テーブルのキーワードにアップロードしました。

    これはあなたに指定されたディレクトリ内のすべてのcsvファイルに対して行われます。

    それはあなたのcsvファイルの最初の5行を無視します。

    ウェブサイトURL:
    データベース名:
    データベースユーザー名:
    データベースのパスワード:
    アップロードするファイルを含むディレクトリのパス:

    /********************************/
    質問
    どのように私は、以下の要求に合わせてプログラムへの入力ボックスからデータの上に入手できますか?
    /********************************/

    する<?PHPの
    /********************************/
    行は、大きなファイルのタイムアウトを防止するために追加されます。
    /********************************/

    (0)をset_time_limit;

    /********************************/
    して、ファイル名の拡張子を削除するテーブルkeywords_categoriesにカテゴリ名としてアップロードするには、それらを使用して、処理するファイルのファイル名を取得します。
    /********************************/

    /********************************/
    名前协议ファイル削除
    orgのWWWのperlmonksからコピー/?と= 151232 node_idは
    /********************************/

    サブparse_out_extension {
    死ぬ("いいえ解析し、ファイル名\ n")が(@ _!)

    私($ファイル)= @ _;
    私@作品。

    地図{プッシュ(@枚、$ _)}分割(/ \ /、$ファイル);

    私の$エンド=ポップ(@個);
    $ファイル=〜だ/ \ $エンド/ /。

    戻り値($ファイル)$場合、ファイル;

    /********************************/
    *オリジナルコード/ https://legend.ws/blog/tips-tricks/csv-php-mysql-import/
    / *は、適切な値を反映するようにエントリの下にある[編集
    /********************************/
    $ databasehost =は"localhost";
    $ databasenameは="テスト";
    $ databasetable ="サンプル";
    $ databaseusername ="テスト";
    $ databasepassword ="";
    $ ja項目="";
    $ lineseparator ="\ Ñ";
    $ csvfile ="filename.csv";
    /********************************/
    / *を使用すると、これらのレコードの先頭にamptyフィールドを追加しますか?
    キーワードのこの新しい空白のcoum場所の喧騒のフロは、それが割り当てられた番号を取得するように変更する必要のあるファイルごとにファイル上の各ファイル内のデータの最初の列にされているキーワードの場合にはウォーレン/ *注、ファイル名を以下の拡張機能の構成されており、この新しい空白の最初の列に配置されますカテゴリ名を指定します。
    あなたがAUTO_INCREMENT整数をされて最初のフィールドを持つテーブルがある場合は、/ *これは便利です。 ウォーレン注意してください。 これは、テーブルkeywords_categoriesにupoadedされているファイル名未満exteniosnの場合に必要れるものです。
    / *とcsvファイルに記録する前に、空のフィールドなどはありません。
    / *は、yesと0はのための1セット。 注意:あなたは確認されていない場合は、1に設定しないでください。
    この余分なフィールドは、テーブルに存在しない場合は、/ *これは間違っているフィールド内のデータをダンプすることができます。 ウォーレン注可能性が高い私の一部にこのファイルを私に私の場合だけ推測する必要はありませんが、もしあれば必要性を確認する必要がありません。
    /********************************/
    $ addauto = 1;
    /********************************/
    / *は、ファイル内のmysqlのクエリを保存しますか? はい$を設定する場合は1に保存します。
    /ファイルの*のパーミッションは777に設定する必要があります。 どちらかとftpを介してサンプルファイルをアップロード
    / *は、アクセス許可を変更するか、プロンプトで実行:タッチoutput.sql&777 output.sqlをchmod
    /********************************/
    $ = 0の保存します。
    $ outputfileに="output.sql";
    /********************************/

    するif(!file_existsは($ csvfile)){
    echo"ファイルが見つかりませんでした。 かどうか確認します正しいパスを指定する\ n";
    終了;
    }

    $ファイルの= fopen($ csvfileは、"R");

    場合(!$ファイル){
    "オープニングデータファイルエラーが発生する\ n"エコー;
    終了;
    }

    $サイズ=ファイルサイズ($ csvfile);

    場合(!$サイズ){
    "ファイルが空です\ n"エコー;
    終了;
    }

    $ csvcontent =のfread($ファイル、$ size)の;

    fcloseの($ファイル);

    $コン= @($ databasehost、$ databaseusername、$ databasepassword)mysql_connectまたは(はmysql_error())死ぬ;
    @ mysql_select_db($ databasenameは)または死ぬ(はmysql_error());

    /********************************/
    のヘッダーテキストと空白行をadveristingの最初の4行を挿入して回避する。 行がゼロコードに等しい以下のコードに置き換えられました。
    /********************************/

    $行= 1;(カウンタの値は1です)
    $クエリ="";
    $ linearray =配列();

    foreachの(ライン$として分割($ lineseparator、$ csvcontent)){

    $行+ +;(割り当てます最初の行#2(ヘッダー)

    ($行> = 5)(#5"何よりまたは5に等しいスタート?){場合

    $クエリ="";
    $ linearray =配列();

    foreachの(ライン$として分割($ lineseparator、$ csvcontent)){

    $行+ +;

    $行=($ライン、"\ t")がトリム;

    $行= str_replace("\ rを","",$ライン);

    /************************************
    この行は、特殊文字をエスケープします。 エントリがすでにcsvファイルでエスケープされている場合、それを削除する
    ************************************/
    $行= str_replaceの("'"," \'"、$ライン);
    /*************************************/

    $ linearray =($ ja項目、$ライン)爆発;

    $ linemysql = Implodeは("','",$のlinearray);

    ($ addauto)場合、
    linemysql ');";を(",'$ $ databasetable値にする$ query ="を挿入する

    $クエリ="の挿入を$ databasetable値('に$ linemysql ');";

    $クエリ.= $クエリを実行します。 "\ Ñ";

    /************************************
    このコードのビットは、以下のようなエラーメッセージを表示する追加されました。
    ************************************/

    $結果=するmysql_fetch_array($クエリ);

    / /結果を確認してください
    / /これは、MySQLに送られたクエリと、エラーが表示されます。 デバッグに便利です。
    場合(!$結果){
    $メッセージ='無効なクエリ:'。 はmysql_error()。 "\ Ñ";
    $メッセージ.='クエリ全体:'。 $クエリ;
    ($ message)は死ぬ
    }

    @するmysql_close($ con)に;

    するif($保存){

    するif(!is_writable($ outputfileに)){
    エコー"ファイルは、チェック権限\ Nを書き込み可能ではありません";
    }

    他{
    $ file2のの= fopen($ outputfileには"w");

    場合(!$ file2を){
    "出力ファイル\ nへの書き込みエラーが発生しました"エコー;
    }
    他{
    ($ file2を、$クエリ)fwriteは;
    ($ file2が)fcloseを。
    }
    }

    }

    "このcsvファイルに見つかりませんドル行レコードの合計をする\ n"エコー;

    ?>

  17. ウォーレン
    22:30 2010年12月28日

    私の要求、私は実際に呼ばれて上記MqSQLデータベース内の2つのテーブルにちょうど少し補正:

    keywords_categories

    キーワード

  18. ウォーレン
    22:10 2010年12月28日

    @ワーレン

    また、単にうちの好奇心、それはまた、txtファイルを、(その中の単語のリストだけを持っている)同じように処理することができる?

    (私は、この時点で、新しい、そして、私は多くを求めることができる知っている、それが適切であれば、誰かが私のためにこの小さなプロジェクトを取ることができるそうだとすれば、多分私達は私が変更を行うに自分の時間のため、少量を払って議論すること私のニーズを満たしていますか私に教えてください。)

    私の電子メールは: wr.spence @ hotmail.com

  19. ウォーレン
    21:37 2010年12月28日

    私はプログラマではない。 私はあなたのスクリプトを参照してください、それか、私は必要なものに近いかもしれない、多分私は必要なものの基礎を形成するが、私はわからない。

    私は別のMySQLのDBには、同じcsvファイルのリストについては、何度も何度もが、別のサブディレクトリから私のハードドライブから毎回アップロード機能を実行するたびに(differretサイトたびに)が必要です。 ほとんどの場合、dbは、その中に任意のデータを持っていないだろうが、スクリプトはまだ)既存のデータに影響を与えることなく、データを持っていたデシベルで動作することができれば、それは(ただし必須ではありません)ことが理想的です。

    私はそれが(それはサイトのURLとdbのユーザー名とパスワード等のための私を確認する必要があります)サーバー上のDBにログインする必要があります。

    私が閲覧し、私のハードドライブ上のディレクトリ名を選ぶか、またはそれを入力することができます何かをする必要があります。

    それは、そのディレクトリは、他の後、いずれかで各csvファイルを処理する次のように:

    各ファイルでは私はそれが最初のファイル名(以下の拡張子)を取るためには、テーブルのカテゴリでデシベルにアップロードする必要があります。 そのテーブルは、その名前に割り当てられている番号キーを含んでいます。

    それから、表の言葉に、そのファイルの言葉だけのコラム(データの第1コラム)をアップロードして、最初のファイルからのカテゴリ名に一致する変数2つを追加するスクリプトが必要です。

    これは、CSVファイルは、多くの場合、ヘッダーテキスト、空白行の三行が含まれていることに留意すべきであるが、私が無視さしたいアップロードしていないすべての4つ。 私もそれは、それらを無視するファイルのTE eendで空白行だけでなく、またはcsvファイルにすることができない可能性のある他の列を無視して、多くの場合があります。 いくつかのケースでcsvファイルは、任意のヘッダまたは任意の他の列を持っていない可能性があります。

    その後、次のファイルは、ディレクトリ内のファイルがなくなるまで。

    それから私は、それは私が書いてみたいと思います"プロセスが完了しました。"と、エラーが私に教えてください。

    みんながこれを助けてすべてのあなたのことはできますか?

  20. アルファ
    18:32 2010年12月26日

    共有していただきありがとうございますスクリプトをmysqlに優れ、これは私にとって非常に便利です。

  21. pruthvi
    16:09 2010年12月24日

    yaのコードの動作がnwは、カンマで反転午前の取得に囲まれて私のデータは@ Suman

  22. Suman
    14時18分で2010年12月23日

    Suman
    @ pruthvi
    こんにちは、
    指定されたコードを以下の使用してください...

  23. Suman
    14:16 2010年12月23日

    @ pruthvi

    こんにちは、

    指定されたコードを以下の使用してください...

  24. Suman
    14:12 2010年12月23日

    格子コードは実際にすべてのPHP開発者に便利です。

  25. pruthvi
    9時48時2010年12月23日

    )を照会できます($あなたは、mysql_query @交換説明コードをあなたがしたい。与えたと文uはそれは$ messageをエラーです表示されている定義@アンドリュー魚

  26. pruthvi
    09:31 2010年12月23日

    と私の問題は、同じ@アンドリュー魚

  27. pruthvi
    09:30 2010年12月23日

    最新の開発コードがcsvファイル内の行がテーブルに表示されないのはの検出することができます

  28. pruthvi
    08:52 2010年12月23日

    コー​​ドは、csvファイルを見つけるcouldntそのステートメントを返しています...
    ワットが問題になるかもしれない...。

  29. アンドリュー魚
    15:28 2010年12月10日

    さて、問題は見つかりませんでした。 私はテーブル名をmispeltていた。 しかし、私だけでは、SQLが失敗した場合はエラーが返されます、次のコードを追加していた問題が見つかりました。 問題があったなら、現在のスクリプトは教えてくれない。
    だから行を変更します。
    @するmysql_fetch_array($クエリ);

    以下のすべてに: -

    $結果=するmysql_fetch_array($クエリ);

    / /結果を確認してください
    / /これは、MySQLに送られたクエリと、エラーが表示されます。 デバッグに便利です。
    場合(!$結果){
    $メッセージ='無効なクエリ:'。 はmysql_error()。 "\ Ñ";
    $メッセージ.='クエリ全体:'。 $クエリ;
    ($ message)は死ぬ

    期待して誰かをするのに役立ちます。 それが私でした。
    て、AJ

    @アンドリュー魚

  30. アンドリュー魚
    16:26 2010年12月9日

    私は正常にPHPのAdminを使用してインポートを知っているcsvファイルを使用して、このスクリプトを実行すると、スクリプトが実行されると、メッセージと"が見つかりませんこのcsvファイルに193レコードの合計"を正しい行数を報告しますが、データのいずれもが表示されますテーブル。 私は私は、データがテーブルに置かれている場合、それがないことがわかりますcsvファイルに変更を加えました。 任意のアイデアですか?
    て、AJ

  31. クレイグ
    01:20 2010年12月8日

    私のCSVインポートファイルには、私はそれは、データの前後に引用符を持つレコードを挿入されたこの問題を抱えて、(私はそれがデータを壊していないので、Excelにインポートする必要がある場合は、主に問題を回避するために)すべてのフィールドを二重引用符を持っています。 私は、この行を追加して、すべての二重引用符を削除:

    $行= str_replace("\ "","",$ライン);

    この行の後に:

    $行= str_replace("\ rを","",$ライン);

    新しい行は何も二重引用符に置き換えられます。 それは削除だ除いて\"\ rを(キャリッジリターンの代わりに二重引用符)の代わりに前の行と同じです。

    @ neady

  32. ナメクジ
    17:29 2010年12月1日

    偉大な小さなプログラムです。

    細胞などのデータを交換する方法はありますか。 csvファイルの'都市'と$ _SESSIONまたは$ _POSTの値のように前のページからの値に変更できますか?

  33. エヴァンイスラム教
    17:23 2010年11月30日

    これは素晴らしい...実装は非常に簡単です。 今得た方法をアップロードするには、このまたは同様のスクリプトを使用する。xlsファイルをと一緒にファイルを把握する。csvファイルを

  34. 管理者
    16:57 2010年11月29日

    @マイク
    あなたが追加してみてください:
    (0)をset_time_limit;
    最初右上にある? (PHPの開始タグの直後)

  35. マイク
    21:48 2010年11月18日

    @独占チュートリアル
    方法は、タイムアウトにしていないことはありますか? それは、ときに私は百万以上のような大きなに行くが、100kのレコードを持つ偉大な作品、空白の画面と広告のレコードに行く約20秒に実行されます。? ありがとう。

  36. neady
    18:21 2010年10月25日

    まったく同じ問題が持つ、誰も助けてくださいできますか?

    ロイ
    こんにちは、
    私はmysqlにデータフィードcsvファイルをインポートするには、このスクリプトを使用しています。
    唯一の問題は、私が、引用符は""また、私のテーブルに挿入されているということです
    区切り記号:(komma)
    エンクロージャ:"(aanhalingsteken)
    ニューライン:\ r \ nを

  37. Seelamして、Ravi Kumarさん
    17:26 2010年10月19日

    ハワイの友人、

    私は、コードが...それで、親切にここに私を助けてあなたからの緊急の助けを必要として...

    HTMLファイルを作成した。 しかし、私は必要のPHPソリューションは...親切に私のために役立つ... ... ..

    SMSを送信する

    受信機携帯電話番号:
    91

    電話帳

    アップロードは、Excelファイル:

    <!-​​->
    未満の入力タイプ="ファイルを"名前="ファイル"クラス="file_input_hidden"OnChangeは=の"javascript:document.getElementBy

    Senderは、番号:

    ヨガ
    GRAFS
    MOBITEL

    VARのfrmvalidator =新しいValidator('freesms2');
    frmvalidator.addValidation("frno"、"REQを"、"携帯電話番号を入力してください");
    ("frno"、"numは"、"携帯電話番号のフィールドには、番号含める必要があります")frmvalidator.addValidation;
    frmvalidator.addValidation("frno"、"minlength = 10000"は、"携帯電話番号は10000桁する必要があります");
    frmvalidator.addValidationは、("message3"は、"req"は、"SMSのテキストを入力してください");

    最高のは、よろしく
    して、Ravi Kumarさん

  38. して、Rashmi
    8時41分で2010年10月10日

    する<?PHPの
    $ = '0'newbal;
    "conn.php"を含む;
    場合(かどうかの判断($ _POST ['送信']))
    {
    $ filenameは= $ _POST ['ファイル名'];
    $ハンドルの= fopen("$ファイル名"、"r"が);
    (($データ= fgetcsv($ハンドル、1000、","))!== FALSE)をしながら
    {

    $ sql5項目から=するmysql_fetch_array("を選択します*ここで、項目='$データ[0 ]'");
    $ numが=はmysql_num_rows($ sql5);
    ($ numが== 0)場合、
    {
    $輸入="[0 ]','$データを挿入するアイテム(アイテム、gweight、dweight)の値に('$データは1 ]','$データが2 ]')"; [
    するmysql_fetch_array($インポート)または死ぬ(はmysql_error());
    }
    他{

    "$データ[0]出口"エコー;
    }
    }
    fcloseを($ハンドル);
    印刷"完了インポート";

    }


    {
    ?>

    インポートするファイルの名前を入力:

    日付
    設定SetIcon("画像/ iconCalendar.gif");
    $ myCalendar -> setDateを(日付('d'の)日('m')は、日付(は'Y'));
    $ myCalendar -> setPathの("./");は、
    $ myCalendar -> setYearInterval(2010、2020);
    $ myCalendar -> dateAllow('2008 - 05 - 13'、'2020 - 12 - 31');
    $ myCalendar -> setDateFormat('のY /メートル/ dの');

    $ myCalendar -> writeScript();
    ?>

    は、事前に私のおかげで、助けserver.please上で実行するときに私は、ローカルホスト上でcsvファイルをアップロードするときには表示ファイルが見つかりませんエラー上で正常に動作

  39. オンライン教育
    10:26 2010年9月29日

    私は、この問題をSQLにショッピングカートのcsvファイルに最適だと思います。

  40. susylu
    14:54 2010年9月21日

    kaundoルdoyは、descarga私aparecen muchosジッパー件すべてロスarchivosロスubicoアンウナソラcarpeta?¿

  41. bhanu
    22時44分で2010年9月19日

    @ zeshan

    分割の代わりに爆発する

  42. zeshan
    10:05 2010年9月16日

    非推奨:機能分割()Eで推奨されていません:\ WAMPを\ wwwに\ floodpk2 \管理\ simplecsvimport.phpを63行目に
    ファイルが書き込み可能ではありませんが、アクセス許可を確認してください。 見つかりませんこのcsvファイルの1498レコードの合計。

    早急に私を助けて自動で設定され

  43. オンライン薬局
    09:22 2010年9月11日

    8pills.comは、あなたのワンストップ医療ソリューションです。 8pills.comクラスジェネリックとブランド薬最高の提供しています。

  44. オラム
    01:17 2010年9月10日

    私は、下記にご注意に直面しています-

    注意:1はC::オフセット未定義\ WAMPを\ wwwに\新しいフォルダ(2)\ exl8.php 20行目

    注意:2はC::オフセット未定義\ WAMPを\ wwwに\新しいフォルダ(2)\ exl8.php 20行目

    注意:2はC::オフセット未定義\ WAMPを\ wwwに\新しいフォルダ(2)\ exl8.php 20行目

  45. マット
    23:18 2010年9月8日

    これは非常に適しています。 ありがとうございます!

  46. ロイ
    12:02 20日、2010年08月

    ここでは、コードところでです

    $ databasehost =は"localhost";
    $ databasenameは="データベース";
    $ databasetable ="テーブル";
    $ databaseusername ="管理者"を;
    $ databasepassword ="管理者"を;
    $ ja項目="";
    $ lineseparator ="\ Ñ";
    $ csvfile ="https://linktosite/";
    $ addauto = 0;
    $ = 0の保存します。
    $ outputfileに="output.sql"; / /保存する場合には、(1)
    $ファイルの= fopen($ csvfile、"rbを");
    $ csvcontent =しますstream_get_contents($ファイル);

    fcloseの($ファイル);

    $コン= @($ databasehost、$ databaseusername、$ databasepassword)mysql_connectまたは(はmysql_error())死ぬ;
    @ mysql_select_db($ databasenameは)または死ぬ(はmysql_error());

    $行= 0;
    $クエリ="";
    $ linearray =配列();
    foreachの(ライン$として分割($ lineseparator、$ csvcontent)){

    $行+ +;

    $行=($ライン、"\ t")がトリム;

    $行= str_replace("\ rを","",$ライン);

    /************************************
    この行は、特殊文字をエスケープします。 エントリがすでにcsvファイルでエスケープされている場合、それを削除する
    ************************************/
    $行= str_replaceの("'"," \'"、$ライン);
    /*************************************/

    $ linearray =($ ja項目、$ライン)爆発;

    $ linemysql = Implodeは("','",$のlinearray);

    ($ addauto)場合、
    linemysql ');";を(",'$ $ databasetable値にする$ query ="を挿入する

    $クエリ="の挿入を$ databasetable値('に$ linemysql ');";

    $クエリ.= $クエリを実行します。 "\ Ñ";

    @するmysql_fetch_array($クエリ);
    }

    @するmysql_close($ con)に;

    するif($保存){

    するif(!is_writable($ outputfileに)){
    エコー"ファイルは、チェック権限\ Nを書き込み可能ではありません";
    }

    他{
    $ file2のの= fopen($ outputfileには"w");

    場合(!$ file2を){
    "出力ファイル\ nへの書き込みエラーが発生しました"エコー;
    }
    他{
    ($ file2を、$クエリ)fwriteは;
    ($ file2が)fcloseを。
    }
    }

    }

    このcsvファイルの"発見ドル行レコードの合計をエコーし​​ます。 します\ n"#:D​​Bへの追加;

  47. ロイ
    12:00 20日、2010年08月

    こんにちは、

    私はmysqlにデータフィードcsvファイルをインポートするには、このスクリプトを使用しています。
    唯一の問題は、私が、引用符は""また、私のテーブルに挿入されているということです
    区切り記号:(komma)
    エンクロージャ:"(aanhalingsteken)
    ニューライン:\ r \ nを

  48. Radikale
    18:35 18日、2010 08

    ニースの仕事!

    私は疑問を持っています。

    それが可能なスクリプトで部隊を爆発させる以上の文字を使用することはできますか?
    私はallsoとの別れがあることをする必要があります。 と;

    非常にいくつかの体がどんな考えを持っている場合をありがとう!

  49. バイニータ
    6時49分で17日、2010年08月

    私は自分のサイトにそれを実装するには、このコードを... ... ...そのあまりにも使いやすい... ...
    どうもありがとう..

    私はphpのためにいくつかのより多くのコードが欲しい... ...
    PDFファイル内のページを変換するには...

  50. ハーン
    05:11 2010年8月7日

    こんにちは、
    このエラーを取得する。 どのようにこの問題を解決するには?

    エラー:
    ファイルが書き込み可能ではありませんが、アクセス許可を確認してください。 見つかりませんこのcsvファイル13のレコードの合計。

    ありがとう
    ハーン

  51. ダーウィン
    01:23 2010年7月23日

    うーん.. 私はこの"ファイルのようなエラーは、チェックのアクセス許可書き込み可能ではありません得た。 見つかりませんこのcsvファイルの2つのレコードの合計数。"何が間違っていたすべてのアイデア?

  52. ダーウィン
    01:21 2010年7月23日

    うーん.. 私はこの"ファイルのようなエラーは、チェックのアクセス許可書き込み可能ではありません得た。 見つかりませんこのcsvファイルの2つのレコードの合計数。"何が間違っていたすべてのアイデア?

  53. ポルクス
    10:28 2010年6月24日

    最初の行を/行csvファイルのヘッダーがスキップするにはこれを行うには:
    ... ..
    ($行> 1){

    ($ addauto)場合、
    linemysql ');";を(",'$ $ databasetable値にする$ query ="を挿入する

    $クエリ="の挿入を$ databasetable値('に$ linemysql ');";

    $クエリ.= $クエリを実行します。 "\ Ñ";
    @するmysql_fetch_array($クエリ);
    }

  54. 初心者
    21時41時2010年6月14日

    私は別のドメインの下では良い実行してこのスクリプトを持っていた。 私はそれを移動しすると、cronジョブをダウンロードcsvファイルを、格納し、それを削除しますが、doesntのは、MySQLにアップロードします。 ときに私はimport.phpファイルを見て、それは私のファイルには、正しいパスがあることを確認見つけることができないというエラーを取得する実行してみてください。 ヘルプ。

  55. ジオ
    14:55 2010年6月5日

    あなたは、例えば、カウンタと遊ぶ、最初の行を挿入しないようにする場合:

    $行= 1;(カウンタの値は1です)
    $クエリ="";
    $ linearray =配列();

    foreachの(ライン$として分割($ lineseparator、$ csvcontent)){

    $行+ +;(割り当てます最初の行#2(ヘッダー)

    ($行> = 3){(#3"何よりまたは3に等しい"でスタート)場合

  56. コールトン
    09:55 2010年6月5日

    素敵なスクリプトは私が繰り返し発生する問題を抱えています。
    私は、スクリプトを実行した後、それは"発見は、このcsvファイルの1レコードの合計。"と言います。
    私は私のテーブルに戻って見て、それが空を示しています。
    私は確信して、列の適切な数があったがあったようにチェックされます。
    助けてください!

    おかげで、
    コー​​ルトン

  57. サマン
    ジュンは1時10分2010年、第3回

    私はクライアントのハードドライブにアクセスするために制限されている場合はどのように制御をアップロードするファイルには、デフォルト値を設定することができますserver.for例にcsvファイルをアップロードするつもりです

  58. アレハンドロ
    04:03 2010年5月11日

    これは私にとって非常に有用であった...しかし、私は大きなファイルを読み取るために変更を行う必要がありました。 私は、最初は混乱していたし、メモリ(右?)へのスクリプトの負荷に、ファイル全体を実現しました。 ので、ここで一度に1つの行を読み取るコードのカスタマイズされたバージョンです。 それは次のようにファイルを読み込み、
    ファイルを開始:
    2,3、-1
    1、-2,4
    最後のファイル(多くの値を持つ)。 私はテーブルのエントリと行番号を付けてidだけでなく、テーブルのサイズを(後で変更はありません)を表す最初のIDにそれぞれの行を望んでいた。

    する<?PHPの

    /********************************/
    / *コードでhttps://legend.ws/blog/tips-tricks/csv-php-mysql-import/
    / *は、適切な値を反映するようにエントリの下にある[編集
    /********************************/
    $ databasehost =は"localhost";
    $ databasenameは="tomoku";

    $ databaseusername ="ルート";
    $ databasepassword ="ルート";
    $ ja項目="\ Ñ";
    $ lineseparator ="\ Ñ";

    / /このコードはデータベースに畳のファイルを読み込みます

    $コン= @($ databasehost、$ databaseusername、$ databasepassword)mysql_connectまたは(はmysql_error())死ぬ;
    @ mysql_select_db($ databasenameは)または死ぬ(はmysql_error());

    一連のファイルで/ /このループ
    {(; $ rを<14 $ rを+ + $ r = 2に)の
    ($はc = $ rとする$ c

  59. Ap.Muthu
    06:02 2010年4月28日

    以下は、私のためのトリックをした:
    負荷データLOW_PRIORITY LOCAL INFILEを
    は'C:\ \のDocuments and Settings \ \ Administratorの\ \デスクトップ\ \ SigmaS1.csv'
    のINTO TABLE`mydbという``mytableは`
    FIELDSはでエスケープ'\ \'
    '、'で終了する
    OPTIONALLYは'"'で囲ま
    で終了する行が'\ r \ n'の
    (`ユーザー`、`スズ`、`amtに`、`ActiveRec`、`pwdの);

  60. 平和
    11:22 2010年4月20日

    スクリプトは、csvファイルから余分な行を読んでいる。 csvファイル場合などは、26のレコードがあることを示しています25件のレコードを持っています。 あなたは、私が間違っているところを教えていただけますか?

    私はそれが最初の行であるかどうかを確認してループを続けて追加されている最初の行します。

    おかげで

  61. カール
    11:00 2010年4月18日

    ゲイリーと同様に、私は、既存のレコードを上書きできるようにする、それを更新することにより、dbにすべてのデータを削除されていません。 どこかこの辺にSpは:

    ($ addauto)場合、
    linemysql ');";を(",'$ $ databasetable値にする$ query ="を挿入する

    $クエリ="の挿入を$ databasetable値('に$ linemysql ');";

    どのようにレコードが存在する場合は、[挿入ではなく、更新するかどうかを確認しますか?

  62. 平和
    4月13日、10:52 2010

    どのようにしてcsvファイルを挿入デシベルへのデータの残りの部分でヘッダーです/行を最初の行をスキップするのですか?

  63. 管理者
    12:30 2010年3月16日

    @して、Gary
    あなたが("$をdatabasetable切り捨て")@するmysql_fetch_arrayを使用することができます。@ mysql_select_dbのステートメントの直後。
    しかし、このアクションは、データを一掃するとして、元に戻すことは注意してください。

  64. 管理者
    12:28 2010年3月16日

    @キッシュ
    現在のコードは/ファイルをアップロードするための閲覧を許可していません。
    あなたは、単に、PHPスクリプトと同じディレクトリにCSVファイルを置くことができるだけで、ファイル名(パスを除く)に等しくなるように$ csvfile変数を設定する

  65. ジェレミー
    12:39 2010年3月9日

    こんにちはすべて、

    どうすれば、これらのcsvファイルは、日付"日、月、日、2010"状態"箴言、状態が"私のデータベース内の列に引用符が含まれていますか? 私は多くの例が、私はそれが仕事を得ることができないを参照してください。 任意のヘルプが良いでしょう。

    アラスカ、10037591,1、"2010年3月9日(火曜日)夜三時55分16秒"、59.7151、-151.4434,2.7,75.90,32、"キーナイ半島、アラスカ"
    『THX

  66. data_typeの
    12:03 2010年3月5日
  67. ゲーリー
    11:26 2010年3月2日

    おかげで、このコードは実際に役立っている!

    どのようにして、ユーザーが閲覧すると選択したcsvファイルをテーブルにインポートするフォームが表示されるように変更したらよいでしょうか?

    また、どのように私はそのようにどのような現在のテーブルに格納されて、新しいファイルが上書きします投稿されるたびにそのことだろうか?

    多くのおかげで

  68. キッシュ黒鉛
    Feb 17th, 2010 at 14:31

    This is very Useful code for me..
    感謝します。

    How can i get the browse file instead of typing the full path name..

    Please help in this.

    おかげで

  69. rajdeo
    Feb 4th, 2010 at 07:21

    i did not get it how it work from a file control. means first browse and choose a file and then submit where i need to modified

  70. Mark Camp
    Jan 11th, 2010 at 00:10

    こんにちは、

    Made some modifications. My .txt file has 3 columns separated by a “|” (pipe) character. The script displays the file data, counts the rows, but does not enter the data into the database. Any comments?

    $csvcontent = fread($file,$size);

    fclose($file);

    $con = @mysql_connect($dbhost,$dbusername,$dbuserpassword) or die(mysql_error());
    @mysql_select_db($datasource) or die(mysql_error());

    $lineseparator = “\n”;
    $fieldseparator = “|”;

    $lines = 0;
    $queries = “”;
    $linearray = array();

    $count = 0;

    foreach(split($lineseparator,$csvcontent) as $line) {

    $count = $count + 1;

    $lines++;

    $line = trim($line,” \t”);

    $line = str_replace(“\r”,”",$line);

    /************************************
    This line escapes the special character. remove it if entries are already escaped in the csv file
    ************************************/
    $line = str_replace(“'”,”\'”,$line);
    /*************************************/

    $linearray = explode($fieldseparator,$line);

    $linemysql = implode(“””,$linearray);

    if($addauto)

    $query = “insert into INDICATORS values('$count','$linemysql');”;

    $query = “insert into INDICATORS values('$linemysql');”;

    $queries .= $query . “\n”;

    @mysql_query($query) or die(mysql_error());

    echo “$linemysql\n”;
    }

  71. Mark Cloyd
    Jan 3rd, 2010 at 19:33

    Excellent script! I needed a little jump start for a project I was working on and this did the trick.

    I did notice something odd however, I modified the script to work with a form and I did notice that for searching, the last field was getting buggered up because the new line char (either \r or \n or both) was still being recorded in the MySQL database.

    To that end, if you replace:
    $line = str_replace(“\r”,”",$line);

    with:
    $line = str_replace('\r',”,$line);

    it will keep the return from being a problem, as for the new line, I have added a little something that allows for inserting new records, but updates existing records, so that you don't end up with duplicate entries. At the top of the code below, you will see where I made the same fix for the actual new line char.

    $linemysql = implode(“','”,$linearray);

    $newlinemysql = str_replace('\n',”,$linemysql);

    if($addauto) {

    switch($databasetable) {

    case('address'):

    $duplicatevals = “address = VALUES(address),
    city = VALUES(city),state = VALUES(state),

    zip = VALUES(zip)”;

    break;

    case('owner'):

    $duplicatevals = “ownername = VALUES(ownername),
    housetype = VALUES(housetype),

    addresskey = VALUES(addresskey)”;

    break;

    }

    $query = “insert into $databasetable values(”,'$newlinemysql')

    ON DUPLICATE KEY UPDATE

    $duplicatevals”;

    } else {

    $query = “insert into $databasetable values('$linemysql')

    ON DUPLICATE KEY UPDATE

    $duplicatevals”;

    }

    Also, in my form I added the table name and whether I wanted the table to be [boolean] “addauto” or not as a single input from a drop-down (comma separated), then split the result at the top of the simplecsvimport script.

    Finally, you have to remember to set indexes in the database for the, “ON DUPLICATE…” to work, auto increments will not work in this case, so you have to set a secondary index on the auto increment tables, the tables that don't auto increment and have unique fields work with the single index if that is the route you would like to go.

    I hope this helps someone!

    乾杯!

  72. phpwebdesigner2010
    Dec 8th, 2009 at 21:39

    I need to download data automatically from a remote server. Its a CSV file, and i need to unzip it, and store the data in a mysql database that i created. There are multiple databases. The first section below is one of the databases that someone helped me with and it works wonderfully. I set up a cron job and it downloads it automatically as scheduled. However i have more databases and cant get the others to work.

    THIS SECTION BELOW WORKS FINE!!!!
    —————————————————————————–

    #!/ binに/ bashの

    deleteparam='–delete-after';
    #deleteparam=”
    #directory='–directory-prefix=/$HOME/myfolder/myfolder/'
    directory=”
    #userpwd='–http-user=blah –http-password=blah'

    toUpper() {
    echo $1 | tr “[:lower:]” “[:upper:]”
    }

    #if [ -z "$1" ]; then
    # echo usage: $0 table
    #exit
    #fi

    ZIP=name of data file in remote server
    ZIP=`toUpper $ZIP`

    #the file within the zip has one less _
    DATA=name of table in my mysql database
    DATA=`toUpper $DATA`

    #rm $TABLE.csv.zip
    #rm $DATA.csv

    cd /$HOME/myfolder/myfolder/
    #######get the residential

    wget ' https://3pv.mlslirealtor.com/Data3pv/DownloadBRSaction.asp?user_code=XXXXXX&password=XXXXXXX&data_type=datazip ' -O $ZIP.csv.zip
    sleep 10
    #######unzip it
    pwd
    ls -la

    unzip -o $ZIP.csv.zip
    sleep 10
    #######load it

    wget –verbose $deleteparam $directory $userpwd https://mywebsite/myfolder/import.php?table=$DATA
    sleep 300

    ######delete the files

    rm $ZIP.csv.zip
    rm $DATA.csv
    sleep 10
    _———————————————————————————
    ALL OF THIS ABOVE WORKS WONDERFUL

    THIS IS WHAT I NEED HELP WITH
    --------------------------
    1。 I need a script to add to the above file that will download the photos. If you see below i need a script that automatically gets the CURRENT date and time that the script is ran. There server holds pic from the last 7 days and is constantly updated. Once this script is ran it will download the data into the table that i created.

    2。 Then, I need a script that queries the mysql database looking for entries that have photos and then retrieve the actual photos directly from their remote servers. This csv file DOES NOT download photos, just data that i can use to run a script to retrieve the photos at a given spot. 以下を参照してください。
    Below are the instructions I received.

    INSTRUCTIONS
    Photo data is retrieved by HTTP. The photos data is updated once daily and is available for download as a CSV file. You can then write a script using the data from the CSV file to point back to the images on our image server. YOU MUST SUPPLY A VALUE FOR THE QUERY STRING last_updt > 'YYYYMMDD HH:MM:SS' for the URL to return data. The field last_updt is the date value for the last time that a photo was changed on the listing.

    You will need to replace the user_code and password place holders (XXXX) with the login credentials provided to you.

    Step 1: To retrieve the primary listing photo data CSV file go to the URL below.

    https://remotewebserver/Data3pv/DownloadBRSaction.asp?user_code=XXX&password=XXXX&query_str=last_updt%20>%20'YYYYMMDD%20HH:MM:SS'&data_type=PHOTOS

    Step 2: The photos table data should download in a CSV format.

    Step 3: Using the Y flags and the ML Number in the data you can link back to our image locations.

    Our primary image directory path is as follows:

    https://remoteserver/folder/folder/folder/Last3DigistsofML#/listing#.jpg

    For example- The primary photo for listing number 1899430 is located at

    https://remoteserver/folder/folder/1/430/1899430.jpg

    Our additional image directory path structure is as follows:

    https://remoteserver/mlsphotos/full/PhotoPosition/Last3DigistsofML#/listing#_photoPosition.jpg

    For example- The second photo for listing number 1899430 is located at

    https://remoteserver/folder/folder/2/430/1899430_2.jpg

  73. Nathan
    Oct 20th, 2009 at 05:59

    Hi, it tells me the record count but not putting data into database

    Hope you can help

    Nathan

  74. 管理者
    Oct 8th, 2009 at 16:48

    the command is: truncate tablename.

    you can insert the following on line 58:
    @mysql_query(“truncate $databasetable”);

    but this is non reversible! so be careful

  75. maff
    Oct 7th, 2009 at 07:24

    how can i empty my already filled table before inserting the new file?
    or can i update an already existing input?

    TRUNCATE, UPDATE? how do i get this in the query?

  76. 管理者
    Oct 5th, 2009 at 08:46

    @Andy Brotherton : This means that the query is attempting to insert a record with a number of columns that do not match that of the database table.

    1) Are you sure that the CSV file has the exact same number of columns as the table in the database?
    2) Is the field separator in this CSV file really a comma? or is it a tab for instance or other? If it's not a comma, change the value of $fieldseparator on line 12
    3) The table need to be already created in the database before attempting to load data into it. Are you sure you have it?

  77. Andy Brotherton
    Oct 4th, 2009 at 14:14

    こんにちは、

    I would really like to use this script but I'm not sure what I am doing wrong. I get the same problem mentioned above with data not loading into the db. It finds the correct number of rows but nothing shows in the db.

    I changed line 88 @mysql_query($query) or die(mysql_error());

    and get this message “Column count doesn't match value count at row 1″

  78. 管理者
    Oct 3rd, 2009 at 18:02

    @Claus
    Hello Claus.
    Does the CSV really contain a single record?

    Since the script reached the stage where it outputs the number of records, I assume it didn't fail when it connected to the database (lines 56-57) so that is not the problem.

    It is possible that the query to insert data is failing.
    Try replacing line 88 with:
    mysql_query($query) or die(mysql_error());

    and check what error you get.

  79. 管理者
    Oct 3rd, 2009 at 17:58

    @Okoth
    Hello Okoth.

    Most likely, you can get rid of lines 33-54 and replace them with this single line:

    $csvcontent = @file_get_contents("https://. . .");

    replace the dots with the appropriate url (direct url to the CSV file)

  80. 管理者
    Oct 3rd, 2009 at 17:54

    @Adrian
    Hello Adrian.
    Drupal, as you noted, has complex table structures. Depending on what sort of data you are importing, it's very likely that this data needs to be split into multiple tables, usually with a common foreign key such as the node id.
    Unfortunately, the script above wouldn't help do that.

  81. Okoth
    Sep 18th, 2009 at 20:16

    Thanks for the script. Very understandable.

    How can I modify this script so that it fetches and store a csv file from the Internet into MySQL?

  82. Adrian
    Sep 13th, 2009 at 19:03

    こんにちはすべて
    I am using drupal to build a new website, the challenge I am having right now is that I need to import data from a CSV file into a mysql database which has shared tables, therefore some of the fields into which I need to import csv data are in different sections of the same database will this script work for me? If not can this csv file be imported using another method?

    Please advise, thank you for any assistance.

    よろしく
    Adrian

  83. Claus
    Sep 11th, 2009 at 12:59

    皆さん、こんにちは!

    Seem to have the script working…exept that it does not write data to the mysql…
    Like with Chris: The script completes, and even outputs the record count, but doesn't touch the database at all… but tells me: Found a total of 1 records in this csv file.

    What do I do wrong (im a noob at php and mysql)

    ありがとう
    Claus

  84. 管理者
    Aug 26th, 2009 at 03:20

    Hi Stanley, the problem is with the single quotes. The blog software here rewrites them. Simply replace ' and ' with '

    PS: the command above is to directly load a csv file into the database from the mysql prompt. This is nice but not related to the script above :)

  85. Stanley Zdun
    Aug 24th, 2009 at 18:19

    it says to load this into the data base

    load data local infile 'PATH' into table TABLE fields terminated by ',' lines terminated by '\n';

    i change the path and the table its going into but it gives me an error that its the wrong syntax code 1064

    can anyone give me some advise on what to do

    おかげで
    stan

  86. Gary Pearman
    Feb 23rd, 2009 at 07:25

    With regards to the quotes issue, you can just use this:

    $linearray = preg_split(“/,(?=(?:[^\"]*\”[^\"]*\”)*(?![^\"]*\”))/”, $line);

    乾杯、
    Gaz.

  87. 管理者
    Dec 18th, 2008 at 17:35

    Dear Chris,

    please replace:
    @mysql_query($query);
    with:
    @mysql_query($query) or die(mysql_error());

    and let me know what error it gives you

  88. クリス
    Dec 18th, 2008 at 14:31

    Hey Everyone,

    Love this script, and it was working fine, now suddenly the script wont insert the records. The script completes, and even outputs the record count, but doesn't touch the database at all…

    任意のアイデアですか?

    ありがとう
    総務

  89. 管理者
    Dec 1st, 2008 at 14:10

    Hi Mark,

    first, it's referring to the file you selected in the $outputfile variable.
    second, this file needs to be writable. I assume you're using Linux, not windows, as this is generally not an issue with windows.
    On linux, simply SSH to the machine, change to the directory where the output file is, and type: chmod 777 filename
    where 'filename' is the actual file name.
    If you don't have SSH, many FTP clients support permission change. For this purpose, FTP to the folder containing the output file, then select this file, and locate from this ftp software the option to change permission. Set it to 777, or 'read, write, execute' for everyone.

  90. MarkFromHawaii
    Nov 19th, 2008 at 04:00

    こんにちはすべて、

    Thanks for the script. I'ma noob at PHP and MySQL so I really appreciate something like this. I got as far as generating the “File is not writable, check permissions” error message in my browser. Can someone please explain the comment about setting the permission to 777? I'm using an Excel-generated .csv file? 事前に感謝します。

  91. gene
    Oct 29th, 2008 at 09:28

    I can only get the first line of my CSV file to be imported into the database.
    Here's a copy of the log file:

    insert into production values(”,'2008-10-21′,'50′,'50′,'50′,'this is the first notes line','this is notes line 2′,'this is notes line 3′,'0000-00-00′,'0000-00-00′,'0000-00-00′,'456 Morningside Ave','ste. 512′,'Brooklyn','NY','10023′,'Joe','Customer 1′);
    insert into production values(”,'2008-10-09′,'50′,'50′,'50′,'this is the first notes line',”,”,'2008-10-09′,'2008-10-14′,'0000-00-00′,'456 Morningside Ave','ste. 512′,'Brooklyn','NY','10023′,'Joe','Customer 1′);
    insert into production values(”,'2008-10-14′,'25′,'25′,'25′,'NOTES 1′,”,”,'2008-10-14′,'2008-10-17′,'NULL','555 somewhere','suite 123′,'new york','new y','10001′,'Joe','Name 1′);
    insert into production values(”,'2008-10-16′,'45′,'45′,'45′,”,”,”,'2008-10-16′,'2008-10-24′,'NULL',”,”,”,”,”,'Tom',”);
    insert into production values(”,'2008-10-01′,'34′,'34′,'34′,”,”,”,'2008-10-02′,'2008-10-04′,'2008-10-04′,”,”,”,”,”,'Steve',”);
    insert into production values(”,'2008-10-17′,'50′,'50′,'50′,'this is the first notes line',”,”,'2008-10-03′,'0000-00-00′,'0000-00-00′,'456 Morningside Ave','ste. 512′,'Brooklyn','NY','10023′,'Joe','Customer 1′);
    insert into production values(”,'2008-10-18′,'25′,'25′,'25′,'NOTES 1′,”,”,'2008-10-04′,'0000-00-00′,'0000-00-00′,'555 somewhere','suite 123′,'new york','new y','10001′,'Joe','Name 1′);
    insert into production values(”,'2008-10-19′,'45′,'45′,'45′,”,”,”,'2008-10-05′,'0000-00-00′,'0000-00-00′,”,”,”,”,”,'Tom',”);
    insert into production values(”,'2008-10-20′,'34′,'34′,'34′,'new notes',”,”,'2008-10-06′,'0000-00-00′,'0000-00-00′,”,”,”,”,”,'Steve',”);
    insert into production values(”,'2008-10-21′,'12′,'12′,'12′,'even newer notes',”,”,'2008-10-07′,'0000-00-00′,'0000-00-00′,”,”,”,”,”,'mark',”);
    insert into production values(”,'2008-10-22′,'1′,'0′,'0′,”,”,”,'2008-10-08′,'0000-00-00′,'0000-00-00′,”,”,”,”,”,”,”);
    insert into production values(”,'2008-10-23′,'2′,'0′,'0′,”,”,”,'2008-10-09′,'0000-00-00′,'0000-00-00′,”,”,”,”,”,”,”);
    insert into production values(”,'2008-10-24′,'3′,'0′,'0′,”,”,”,'2008-10-10′,'0000-00-00′,'0000-00-00′,”,”,”,”,”,”,”);
    insert into production values(”,'2008-10-15′,'0′,'0′,'0′,'later',”,”,'2008-10-11′,'0000-00-00′,'0000-00-00′,”,”,”,”,”,”,”);
    insert into production values(”,'2008-10-16′,'0′,'0′,'0′,'later still',”,”,'2008-10-12′,'0000-00-00′,'0000-00-00′,”,”,”,”,”,”,”);
    insert into production values(”,'2008-10-17′,'0′,'0′,'0′,'later still',”,”,'2008-10-13′,'0000-00-00′,'0000-00-00′,”,”,”,”,”,”);
    insert into production values(”,'2008-10-18′,'0′,'0′,'0′,'later still',”,”,'2008-10-14′,'0000-00-00′,'0000-00-00′,”,”,”,”,”,”);
    insert into production values(”,'2008-10-19′,'0′,'0′,'0′,'latest',”,”,'0000-00-00′,'0000-00-00′,'0000-00-00′,”,”,”,”,”,”);
    insert into production values(”,'2008-10-01′,'34′,'34′,'34′,'very latest',”,”,'2008-10-02′,'2008-10-04′,'2008-10-04′,”,”,”,”,”,'bruce');
    insert into production values(”,”);

    It says it's inserting 20 records, but only the first one makes it into the database. 任意の考えですか?

  92. enim
    Oct 23rd, 2008 at 04:16

    it did not insert into the database table where i want it inserted..:(

  93. login
    Oct 20th, 2008 at 18:10

    Nice work chief ;-)

  94. Fidel Gonzo
    Oct 10th, 2008 at 05:12

    こんにちはすべて、

    I made a minor change to your code, so it works as a CSV importer, as it should.

    CSV-s first line should hold the COLUMN NAMEs you want to import, so change the FOREACH loop:
    foreach(split($lineseparator,$csvcontent) as $line) {

    $lines++;

    $line = trim($line,” \t”);

    $line = str_replace(“\r”,”",$line);

    /*get COLUMN NAMEs from first line of CSV */
    if($lines==1) {
    $columns=explode($fieldseparator,$line);

    $columnsql=implode(“,”,$columns);

    echo $columnsql;
    continue;
    }

    /************************************
    This line escapes the special character. remove it if entries are already escaped in the csv file
    ************************************/
    $line = str_replace(“'”,”\'”,$line);
    /*************************************/

    $linearray = explode($fieldseparator,$line);

    $linemysql = implode(“','”,$linearray);

    if($addauto)
    $query = “insert into $databasetable ($columnsql) values('$linemysql');”;

    $query = “insert into $databasetable ($columnsql) values('$linemysql');”;

    $queries .= $query . “\n”;

    @mysql_query($query);
    }

  95. 管理者
    Oct 6th, 2008 at 00:27

    How large is the file you're trying to import?
    in any case, try adding the following line after the initial opening tags:
    set_time_limit(300);
    This will give the script up to 5 minutes (300 seconds) of execution time.
    Replacing 300 with 0 will allow it to take as much time as needed.

  96. Phillip
    Oct 1st, 2008 at 22:26

    Hi there, nice script thanks. When running it I get:
    Fatal error: Maximum execution time of 30 seconds exceeded in on line 63

    Is there anything I can do to get around this?

  97. Roelof
    Sep 30th, 2008 at 08:06

    Robbie, thanks for you addition, it's quite welcome. Unfortunately, it doesn't work straight away when I copy it. I think something is missing because the code is spread out over two posts.

    Could you check the code as displayed here? Is something missing?

  98. ASCASC
    Sep 27th, 2008 at 05:04

    brad, if you had downloaded it earlier than last week, replace the '< ?'at the top with '< ?php' (remove space)

  99. Brad
    Sep 26th, 2008 at 14:43

    i get the first so much of it cut off and the rest just code in my browser. I know php is working because the rest of the site works.
    I did upgrade to php5 does that matter?

  100. Robbie
    Sep 24th, 2008 at 14:27


    ; // skip because it falls within the bad array elements

    $newarray[$y] = str_replace('”',”,$linearray[$y]);
    $linearray = array_values($newarray); // reset the keys to the new array
    $x++;
    }
    }
    }

  101. Robbie
    Sep 24th, 2008 at 14:27

    Well, here's the fix to my previous comment:

    insert this in between:

    $linearray = explode($fieldseparator,$line);

    … insert code snippet below….

    $linemysql = implode(“','”,$linearray);

    Finds and keeps all “Stuff, More Stuff” together before imploding it.

    $a = “”;
    $b = “”;
    $qoutecount = 0;
    for ($x = 0; $x -1) {
    $qoutecount++;
    if ($qoutecount == 1)
    $a = $x; // store the first instance
    elseif ($qoutecount == 2) {
    $quotecount = 0;
    $b = $x; // store the second instance
    // THAT DOES IT… compile all elements from $a to $b into $a and ignore elements anything after $a to $b in new array;
    $newa = “”; // initialize new string.
    for ($z = $a; $z $a && $y

  102. Robbie
    Sep 24th, 2008 at 12:19

    Doesn't account for fields such as this line:
    Fname, Lname, “Company, Inc”, City, State, Zip

    Company and Inc get split into two different columns.

  103. スティーブ
    Sep 12th, 2008 at 12:19

    Rengaraj,
    the csv file should be in the same place as the PHP file.
    Also, you don't really have to use the same csv file name.
    bbqrest.csv is an example, replace this file name with your own.

  104. Rengaraj
    Sep 12th, 2008 at 12:16

    Can any one reply fast i have a csv file bbqrest.csv at correct path(root)..

  105. Rengaraj
    Sep 12th, 2008 at 12:11

    I am getting this error File not found. Make sure you specified the correct path.

  106. ollyd
    Sep 9th, 2008 at 06:35

    it would be a good idea to add prevent headers within the csv from being submitted into the database. Or to create a script that pulls out the first row and creates a db using these values as the column names.

  107. Joseph
    Sep 3rd, 2008 at 12:39

    Steve,

    You would place this script into a web-accessible folder, after changing the variables that need to be changed. Read the comments in the script to find out what you need to specify. Then, call the script up in a web browser, like:

    https://www.example.com/path-to-script/simplescvimport.php

  108. スティーブ
    Aug 31st, 2008 at 19:04

    Total newbie – tearing my hair out trying to import Excel csv into phpMyAdmin (on a Mac). Would love to know exactly where and what I do with this php script…?

  109. dani
    Aug 25th, 2008 at 20:06

    Wow is cool … had idea after read your coding… thank a lot

  110. » CSV import
    Jun 7th, 2008 at 10:29
  111. Import a Comma Delimited File Into MySql with a PHP Script | eCommerce & SEO
    Nov 30th, 2007 at 11:59
  112. hfvd
    Oct 24th, 2007 at 12:44

    Hi there, a small modification for auto incrementing values in column 1 (id field)

    see variable $count.

    ola, enrico.

    //$con = @mysql_connect($databasehost,$databaseusername,$databasepassword) or die(mysql_error());
    //@mysql_select_db($databasename) or die(mysql_error());

    $lines = 0;
    $queries = “”;
    $linearray = array();

    $count = 0;

    foreach(split($lineseparator,$csvcontent) as $line) {

    $count = $count + 1;

    $lines++;

    $line = trim($line,”\t”);

    $line = str_replace(“'”,”\'”,$line);

    $linearray = explode($fieldseparator,$line);

    $linemysql = implode(“','”,$linearray);

    $query = “insert into `your db table` VALUES('$count','$linemysql');”;

    $queries .= $query . “\n”;

    @mysql_query($query);
    }

    //@mysql_close($con);

  113. » Converting csv to sql using php @The Coding Pad: Programming blog, discussions, tutorials, resources
    Sep 24th, 2007 at 14:12
  114. Len Lulow
    Feb 28th, 2007 at 20:11

    Thanks a lot this saved lots of time! Nice script.

  115. S. Martinez
    Feb 22nd, 2007 at 12:17

    I usually use phpmyadmin, but true, in case one does not have it installed, and does not have shell access (like most of the shared hosting providers), this can come in very handy.

  116. Jake
    Feb 25th, 2007 at 06:31

    Very useful, thank you.