2016-10-20 10 views
0

オリジナル投稿の編集。私は答えを見つけた!!!!ヘルプ:)phpバージョン4のmysqlクエリをcsvにエクスポート

と私は今、コメント欄でこの上の助言に感謝して以下のコードを使用してこの作業を持っている:

<?php 

$f = fopen('incident_csv\test.csv', 'w'); 

$query = " 
select column1, column2, column3 
from table 
where columns = values 
"; 

$var1 = mysql_query($query, $database connection variable); 

/* From Monkey Zeus */ 

$csv_lines = array(); 

// Loop your records from the DB 
while ($row = mysql_fetch_assoc($var1)){ 

$columns = array(); 

// Loop each column for the row 
foreach($row as $k=>$v){ 
// Surround column item in double-quotes and escape double-quotes with double-double-quotes 
$columns[] = '"'.str_replace('"', '""', $v).'"'; 
} 

// Join on a comma 
$csv_lines[] = implode(',', $columns); 
} 

// Create full CSV file by joining on a newline 
$csv_file_string = implode("\n", $csv_lines); 

/* From Monkey Zeus */ 

fwrite($f, $csv_file_string); 

?> 
+3

PHP 4を離れて8月、2008年以来の実行セキュリティ更新プログラムを持っていません。遠く離れて走りなさい。 – Quentin

+0

どの機能が 'fpost'ですか?あなたはそれを書きましたか?そして 'fopen'から返されたファイルリソースをどこにも保存しませんでした。このように使用することはできません。 –

+0

@Quentin OPが企業インフラストラクチャの複数の側面に影響を与えるために分岐した可能性のある古いシステムの境界内で作業している可能性は非常に高いです。これがよく知られているウェブサイトを動かすために使用されていない限り、PHP4は上記の目標を達成するために完全に利用可能でなければなりません。 – MonkeyZeus

答えて

0

あなたはこれを行うことができます。

$csv_lines = array(); 

// Loop your records from the DB 
while ($row = mysql_fetch_assoc($var1)){ 

    $columns = array(); 

    // Loop each column for the row 
    foreach($row as $k=>$v){ 
     // Surround column item in double-quotes and escape double-quotes with double-double-quotes 
     $columns[] = '"'.str_replace('"', '""', $v).'"'; 
    } 

    // Join on a comma 
    $csv_lines[] = implode(',', $columns); 

    // Write to the file right away. You are using PHP4 so I imagine system memory is not plentiful 
    // If you use this then there is no need for the "$csv_lines[] =" from above 
    // nor the $csv_file_string after the while loop 
    // fwrite($f, implode(',', $columns)."\n"); 
} 

// fclose($f); // If you choose to write to the file during the while loop then close the file handle when you are finished 

// Create full CSV file by joining on a newline 
$csv_file_string = implode("\n", $csv_lines); 
+0

この提案のための@MonkeyZeus私は元のクエリにこれを縫い合わせて、 "mysql_fetch_array"を "mysql_fetch_assoc"に変更してCSVファイル内の重複を削除することで、この問題を解決しました。 –

+0

ニース!お役に立てて嬉しいです。しかし、PHP4のサーバにはメモリがあまりないので、ディスクに書き込むことはできないと思いますので、 'fwrite($ f、implode( '、'、$ columns)。\ n");おそらく望ましい。 – MonkeyZeus

0

することはでき単にWITE arrayToCsv機能:

function arrayToCsv($array) { 
    $string = array(); 

    foreach ($array as $field) { 
     $string[] = implode(';', $field); 
    } 

    return implode("\n", $string); 
} 

$a = array(
    array('First Field of First Line', 'Second Field of First Line'), 
    array('First Field of Second Line', 'Second Field of Second Line') 
); 
fwrite($f, arrayToCsv($a)); 

しかし、あなたの$f$f = fopen('file location', 'w');

から設定してください。

+0

CSVのCは、通常、セミコロンではなく** C ** mmaを表します。 – MonkeyZeus

+0

Excelはセミコロンでのみ正しく開きます。 ;-) しかし、必要に応じて簡単に変更できます。 –

+0

このアドバイスをいただきありがとうございますが、私はこのメソッドを働かせるために苦労しました。私はまだ、常に2つの方法で何かをする方法を知っているように努力します:) –

関連する問題