PHPスクリプトは、MySQLにCSVデータをインポートする
これは、あなたのデータベースにCSVデータをインポートできるようにする簡単なスクリプトです。 あなたは、単に適切なフィールドを編集することができますので、csvファイルと一緒にアップロードして、Webサイトからそれを呼び出して、それが残りの部分を行いますこれは便利だ。
それは昏睡状態であるかどうか、このcsvファイルの区切り文字を指定することができます、タブなどがこれまた、行区切り文字を選択することができ、あなたはファイル(データをSQLダンプとも呼ばれる)に出力を保存することができます。
また、通常、主キーの自動インクリメントの整数であり、各行の先頭に空のフィールドを含めることができます。
あなたはphpmyadminの持っていない主場合、またはには、ログインの手間を必要としない、数回のクリックのソリューションを望んでいるこのスクリプトは有用であるか、単にコマンドプロンプトで男です。
ちょうどテーブルが既にデータをダンプする前に作成されていることを確認します。
あなたがバグレポートを得た場合、親切にご意見を投稿してください。
素晴らしい脚本! tnxにTNXたくさん
私がこの記事を...本当に私はこのポストから感銘を受けて.. ...感謝して任意の単語を持っていない。それは、これを共有するための素晴らしい人間..おかげされたこの記事を作成する人
私たち。
ああ...有益なポストは本当にthatsの。 私は新しいブロガーにそれは私のために必ず役立つ情報になるよ。 共有をありがとう!
スクリプトは、かなりのとおりです私にとっては素晴らしい作品。 感謝します!
こんにちは...
私は問題に直面して、私はこれは、そのエラーを言って表示を使用する場合
"非推奨:機能分割()C言語で推奨されていません:\ WAMPを\ wwwに\をtest1 \ simplecsvimport.phpを64行で
ファイルが書き込み可能ではありませんが、アクセス許可を確認してください。 見つかりませんこのcsvファイル41のレコードの合計。 "
Plsの回答は、間違っているんです!
事前に感謝!
私は本当にこのコードで混乱しています!
しかし、私は試していただきたい!
ところで、コードまたはフィールドのHTMLは、このコードを呼び出す方法
おかげで、非常に良いcsvファイルのインポーターです。
しかし、私は一つの小さな問題を発見、それだけで構造とcsvファイルで動作します:"フィールド1"は、"フィールド2"は、"フィールド3"などが、私はこのようなCSVファイルをhava:フィールド1;フィールド2; filed3;等
私は変更することができる唯一のことはja項目$であるどのようにファイルが(フィールド区切り)"を使用していないことをスクリプトを'伝える'ことができますか?
マイideia、または$を持つコンボボックスja項目(された 別の$ 1つをfielddelimiter("またはなし)
事前に感謝します。
それらのコンマでフィールドが新しいエントリとして読んで取得しないように二重引用符で囲まれているフィールドのために働くために、このスクリプトを取得する方法のいずれかのアイデア?
私は、ドル金額は、ユーザーは、カンマを使用している可能性がありますコメント欄のようにコンマが含まれているデータを持っている。 私は、インポート、フィールド全体を必要とする、このスクリプトは、新しいフィールドとして扱います。
マーククロイド@
"アドレス=の値(アドレス)
'='予期しない構文エラー:私はパースエラーとしてここでエラーを取得する
@ 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
私は実際に動作するようにこれを取得するいくつかのヘルプを使用することができます...
ロン
@ 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)に;
ありがとう、
@にして、Khaled
どうすればUTF8をエクスポートすることができますoutput.sqlに秀でる
おかげで
私はスクリプトを試してみましたoutput.sqlは奇妙なものを書いであり、
!????????????test_excel値('主キーを挿入|?質問K5のqは質問K5のqは質問K5のqが...。
これらのことのように
どのように私はこの問題を修正することができます。 に何か特別な形式は、エクセルシート?
こんにちはみんなは、ちょうどあなたがすべて私は私が必要とする実際のプログラムの書き込みに誰かを見つけたことを知ってもらいたい。
@ワーレン
/********************************/
このファイルには、なるように、彼が必要とするものにこのプログラムを作成するソースサイトおよびその他の情報のコメントに基づいて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"エコー;
?>
私の要求、私は実際に呼ばれて上記MqSQLデータベース内の2つのテーブルにちょうど少し補正:
keywords_categories
キーワード
@ワーレン
また、単にうちの好奇心、それはまた、txtファイルを、(その中の単語のリストだけを持っている)同じように処理することができる?
(私は、この時点で、新しい、そして、私は多くを求めることができる知っている、それが適切であれば、誰かが私のためにこの小さなプロジェクトを取ることができるそうだとすれば、多分私達は私が変更を行うに自分の時間のため、少量を払って議論すること私のニーズを満たしていますか私に教えてください。)
私の電子メールは: wr.spence @ hotmail.com
私はプログラマではない。 私はあなたのスクリプトを参照してください、それか、私は必要なものに近いかもしれない、多分私は必要なものの基礎を形成するが、私はわからない。
私は別のMySQLのDBには、同じcsvファイルのリストについては、何度も何度もが、別のサブディレクトリから私のハードドライブから毎回アップロード機能を実行するたびに(differretサイトたびに)が必要です。 ほとんどの場合、dbは、その中に任意のデータを持っていないだろうが、スクリプトはまだ)既存のデータに影響を与えることなく、データを持っていたデシベルで動作することができれば、それは(ただし必須ではありません)ことが理想的です。
私はそれが(それはサイトのURLとdbのユーザー名とパスワード等のための私を確認する必要があります)サーバー上のDBにログインする必要があります。
私が閲覧し、私のハードドライブ上のディレクトリ名を選ぶか、またはそれを入力することができます何かをする必要があります。
それは、そのディレクトリは、他の後、いずれかで各csvファイルを処理する次のように:
各ファイルでは私はそれが最初のファイル名(以下の拡張子)を取るためには、テーブルのカテゴリでデシベルにアップロードする必要があります。 そのテーブルは、その名前に割り当てられている番号キーを含んでいます。
それから、表の言葉に、そのファイルの言葉だけのコラム(データの第1コラム)をアップロードして、最初のファイルからのカテゴリ名に一致する変数2つを追加するスクリプトが必要です。
これは、CSVファイルは、多くの場合、ヘッダーテキスト、空白行の三行が含まれていることに留意すべきであるが、私が無視さしたいアップロードしていないすべての4つ。 私もそれは、それらを無視するファイルのTE eendで空白行だけでなく、またはcsvファイルにすることができない可能性のある他の列を無視して、多くの場合があります。 いくつかのケースでcsvファイルは、任意のヘッダまたは任意の他の列を持っていない可能性があります。
その後、次のファイルは、ディレクトリ内のファイルがなくなるまで。
それから私は、それは私が書いてみたいと思います"プロセスが完了しました。"と、エラーが私に教えてください。
みんながこれを助けてすべてのあなたのことはできますか?
共有していただきありがとうございますスクリプトをmysqlに優れ、これは私にとって非常に便利です。
yaのコードの動作がnwは、カンマで反転午前の取得に囲まれて私のデータは@ Suman
@ pruthvi
こんにちは、
指定されたコードを以下の使用してください...
格子コードは実際にすべてのPHP開発者に便利です。
)を照会できます($あなたは、mysql_query @交換説明コードをあなたがしたい。与えたと文uはそれは$ messageをエラーです表示されている定義@アンドリュー魚
と私の問題は、同じ@アンドリュー魚
最新の開発コードがcsvファイル内の行がテーブルに表示されないのはの検出することができます
コードは、csvファイルを見つけるcouldntそのステートメントを返しています...
ワットが問題になるかもしれない...。
さて、問題は見つかりませんでした。 私はテーブル名をmispeltていた。 しかし、私だけでは、SQLが失敗した場合はエラーが返されます、次のコードを追加していた問題が見つかりました。 問題があったなら、現在のスクリプトは教えてくれない。
だから行を変更します。
@するmysql_fetch_array($クエリ);
以下のすべてに: -
$結果=するmysql_fetch_array($クエリ);
/ /結果を確認してください
/ /これは、MySQLに送られたクエリと、エラーが表示されます。 デバッグに便利です。
場合(!$結果){
$メッセージ='無効なクエリ:'。 はmysql_error()。 "\ Ñ";
$メッセージ.='クエリ全体:'。 $クエリ;
($ message)は死ぬ
期待して誰かをするのに役立ちます。 それが私でした。
て、AJ
@アンドリュー魚
私は正常にPHPのAdminを使用してインポートを知っているcsvファイルを使用して、このスクリプトを実行すると、スクリプトが実行されると、メッセージと"が見つかりませんこのcsvファイルに193レコードの合計"を正しい行数を報告しますが、データのいずれもが表示されますテーブル。 私は私は、データがテーブルに置かれている場合、それがないことがわかりますcsvファイルに変更を加えました。 任意のアイデアですか?
て、AJ
私のCSVインポートファイルには、私はそれは、データの前後に引用符を持つレコードを挿入されたこの問題を抱えて、(私はそれがデータを壊していないので、Excelにインポートする必要がある場合は、主に問題を回避するために)すべてのフィールドを二重引用符を持っています。 私は、この行を追加して、すべての二重引用符を削除:
$行= str_replace("\ "","",$ライン);
この行の後に:
$行= str_replace("\ rを","",$ライン);
新しい行は何も二重引用符に置き換えられます。 それは削除だ除いて\"\ rを(キャリッジリターンの代わりに二重引用符)の代わりに前の行と同じです。
@ neady
偉大な小さなプログラムです。
細胞などのデータを交換する方法はありますか。 csvファイルの'都市'と$ _SESSIONまたは$ _POSTの値のように前のページからの値に変更できますか?
これは素晴らしい...実装は非常に簡単です。 今得た方法をアップロードするには、このまたは同様のスクリプトを使用する。xlsファイルをと一緒にファイルを把握する。csvファイルを
@マイク
あなたが追加してみてください:
(0)をset_time_limit;
最初右上にある? (PHPの開始タグの直後)
@独占チュートリアル
方法は、タイムアウトにしていないことはありますか? それは、ときに私は百万以上のような大きなに行くが、100kのレコードを持つ偉大な作品、空白の画面と広告のレコードに行く約20秒に実行されます。? ありがとう。
まったく同じ問題が持つ、誰も助けてくださいできますか?
ハワイの友人、
私は、コードが...それで、親切にここに私を助けてあなたからの緊急の助けを必要として...
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さん
する<?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ファイルをアップロードするときには表示ファイルが見つかりませんエラー上で正常に動作
私は、この問題をSQLにショッピングカートのcsvファイルに最適だと思います。
kaundoルdoyは、descarga私aparecen muchosジッパー件すべてロスarchivosロスubicoアンウナソラcarpeta?¿
@ zeshan
分割の代わりに爆発する
非推奨:機能分割()Eで推奨されていません:\ WAMPを\ wwwに\ floodpk2 \管理\ simplecsvimport.phpを63行目に
ファイルが書き込み可能ではありませんが、アクセス許可を確認してください。 見つかりませんこのcsvファイルの1498レコードの合計。
早急に私を助けて自動で設定され
8pills.comは、あなたのワンストップ医療ソリューションです。 8pills.comクラスジェネリックとブランド薬最高の提供しています。
私は、下記にご注意に直面しています-
注意:1はC::オフセット未定義\ WAMPを\ wwwに\新しいフォルダ(2)\ exl8.php 20行目
注意:2はC::オフセット未定義\ WAMPを\ wwwに\新しいフォルダ(2)\ exl8.php 20行目
注意:2はC::オフセット未定義\ WAMPを\ wwwに\新しいフォルダ(2)\ exl8.php 20行目
これは非常に適しています。 ありがとうございます!
ここでは、コードところでです
$ 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"#:DBへの追加;
こんにちは、
私はmysqlにデータフィードcsvファイルをインポートするには、このスクリプトを使用しています。
唯一の問題は、私が、引用符は""また、私のテーブルに挿入されているということです
区切り記号:(komma)
エンクロージャ:"(aanhalingsteken)
ニューライン:\ r \ nを
ニースの仕事!
私は疑問を持っています。
それが可能なスクリプトで部隊を爆発させる以上の文字を使用することはできますか?
私はallsoとの別れがあることをする必要があります。 と;
非常にいくつかの体がどんな考えを持っている場合をありがとう!
私は自分のサイトにそれを実装するには、このコードを... ... ...そのあまりにも使いやすい... ...
どうもありがとう..
私はphpのためにいくつかのより多くのコードが欲しい... ...
PDFファイル内のページを変換するには...
こんにちは、
このエラーを取得する。 どのようにこの問題を解決するには?
エラー:
ファイルが書き込み可能ではありませんが、アクセス許可を確認してください。 見つかりませんこのcsvファイル13のレコードの合計。
ありがとう
ハーン
うーん.. 私はこの"ファイルのようなエラーは、チェックのアクセス許可書き込み可能ではありません得た。 見つかりませんこのcsvファイルの2つのレコードの合計数。"何が間違っていたすべてのアイデア?
うーん.. 私はこの"ファイルのようなエラーは、チェックのアクセス許可書き込み可能ではありません得た。 見つかりませんこのcsvファイルの2つのレコードの合計数。"何が間違っていたすべてのアイデア?
最初の行を/行csvファイルのヘッダーがスキップするにはこれを行うには:
... ..
($行> 1){
($ addauto)場合、
linemysql ');";を(",'$ $ databasetable値にする$ query ="を挿入する
他
$クエリ="の挿入を$ databasetable値('に$ linemysql ');";
$クエリ.= $クエリを実行します。 "\ Ñ";
@するmysql_fetch_array($クエリ);
}
私は別のドメインの下では良い実行してこのスクリプトを持っていた。 私はそれを移動しすると、cronジョブをダウンロードcsvファイルを、格納し、それを削除しますが、doesntのは、MySQLにアップロードします。 ときに私はimport.phpファイルを見て、それは私のファイルには、正しいパスがあることを確認見つけることができないというエラーを取得する実行してみてください。 ヘルプ。
あなたは、例えば、カウンタと遊ぶ、最初の行を挿入しないようにする場合:
$行= 1;(カウンタの値は1です)
$クエリ="";
$ linearray =配列();
foreachの(ライン$として分割($ lineseparator、$ csvcontent)){
$行+ +;(割り当てます最初の行#2(ヘッダー)
($行> = 3){(#3"何よりまたは3に等しい"でスタート)場合
素敵なスクリプトは私が繰り返し発生する問題を抱えています。
私は、スクリプトを実行した後、それは"発見は、このcsvファイルの1レコードの合計。"と言います。
私は私のテーブルに戻って見て、それが空を示しています。
私は確信して、列の適切な数があったがあったようにチェックされます。
助けてください!
おかげで、
コールトン
私はクライアントのハードドライブにアクセスするために制限されている場合はどのように制御をアップロードするファイルには、デフォルト値を設定することができますserver.for例にcsvファイルをアップロードするつもりです
これは私にとって非常に有用であった...しかし、私は大きなファイルを読み取るために変更を行う必要がありました。 私は、最初は混乱していたし、メモリ(右?)へのスクリプトの負荷に、ファイル全体を実現しました。 ので、ここで一度に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
以下は、私のためのトリックをした:
負荷データLOW_PRIORITY LOCAL INFILEを
は'C:\ \のDocuments and Settings \ \ Administratorの\ \デスクトップ\ \ SigmaS1.csv'
のINTO TABLE`mydbという``mytableは`
FIELDSはでエスケープ'\ \'
'、'で終了する
OPTIONALLYは'"'で囲ま
で終了する行が'\ r \ n'の
(`ユーザー`、`スズ`、`amtに`、`ActiveRec`、`pwdの);
スクリプトは、csvファイルから余分な行を読んでいる。 csvファイル場合などは、26のレコードがあることを示しています25件のレコードを持っています。 あなたは、私が間違っているところを教えていただけますか?
私はそれが最初の行であるかどうかを確認してループを続けて追加されている最初の行します。
おかげで
ゲイリーと同様に、私は、既存のレコードを上書きできるようにする、それを更新することにより、dbにすべてのデータを削除されていません。 どこかこの辺にSpは:
($ addauto)場合、
linemysql ');";を(",'$ $ databasetable値にする$ query ="を挿入する
他
$クエリ="の挿入を$ databasetable値('に$ linemysql ');";
どのようにレコードが存在する場合は、[挿入ではなく、更新するかどうかを確認しますか?
どのようにしてcsvファイルを挿入デシベルへのデータの残りの部分でヘッダーです/行を最初の行をスキップするのですか?
@して、Gary
あなたが("$をdatabasetable切り捨て")@するmysql_fetch_arrayを使用することができます。@ mysql_select_dbのステートメントの直後。
しかし、このアクションは、データを一掃するとして、元に戻すことは注意してください。
@キッシュ
現在のコードは/ファイルをアップロードするための閲覧を許可していません。
あなたは、単に、PHPスクリプトと同じディレクトリにCSVファイルを置くことができるだけで、ファイル名(パスを除く)に等しくなるように$ csvfile変数を設定する
こんにちはすべて、
どうすれば、これらのcsvファイルは、日付"日、月、日、2010"状態"箴言、状態が"私のデータベース内の列に引用符が含まれていますか? 私は多くの例が、私はそれが仕事を得ることができないを参照してください。 任意のヘルプが良いでしょう。
アラスカ、10037591,1、"2010年3月9日(火曜日)夜三時55分16秒"、59.7151、-151.4434,2.7,75.90,32、"キーナイ半島、アラスカ"
『THX
おかげで、このコードは実際に役立っている!
どのようにして、ユーザーが閲覧すると選択したcsvファイルをテーブルにインポートするフォームが表示されるように変更したらよいでしょうか?
また、どのように私はそのようにどのような現在のテーブルに格納されて、新しいファイルが上書きします投稿されるたびにそのことだろうか?
多くのおかげで
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.
おかげで
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
こんにちは、
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”;
}
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!
乾杯!
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
Hi, it tells me the record count but not putting data into database
Hope you can help
Nathan
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
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?
@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?
こんにちは、
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″
@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.
@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)
@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.
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?
こんにちはすべて
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
皆さん、こんにちは!
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
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
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
With regards to the quotes issue, you can just use this:
$linearray = preg_split(“/,(?=(?:[^\"]*\”[^\"]*\”)*(?![^\"]*\”))/”, $line);
乾杯、
Gaz.
Dear Chris,
please replace:
@mysql_query($query);
with:
@mysql_query($query) or die(mysql_error());
and let me know what error it gives you
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…
任意のアイデアですか?
ありがとう
総務
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.
こんにちはすべて、
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? 事前に感謝します。
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. 任意の考えですか?
it did not insert into the database table where i want it inserted..:(
Nice work chief
こんにちはすべて、
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);
}
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.
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?
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?
brad, if you had downloaded it earlier than last week, replace the '< ?'at the top with '< ?php' (remove space)
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?
)
; // 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++;
}
}
}
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
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.
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.
Can any one reply fast i have a csv file bbqrest.csv at correct path(root)..
I am getting this error File not found. Make sure you specified the correct path.
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.
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
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…?
Wow is cool … had idea after read your coding… thank a lot
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);
Thanks a lot this saved lots of time! Nice script.
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.
Very useful, thank you.