mySQL DB からデータを取得する CSV ファイルを作成する必要があります。
実際には、次のようにデータを書き込むだけでなく、CSV tp をラベル付きで修正する必要があります。
id,name,url
1,thisismyname,thisismyurl
よく整理された CSV ファイルが必要で、各データが相対列に挿入されます。
また、これから追加する関数では、DBからデータを取得してそのままCSVファイルに書き込むしかありません。ただし、データを操作して、CSV に次のようにラベルを付ける必要があります。
Campaign Name:
Name of the campaign
Campaign Url:
Url of the campaign
Tot visits:
Tot of visits
Tot unique visits:
Tot of unique visits
id name url
1 thisname this url
2 thisname this url
3 thisname this url
4 thisname this url
5 thisname this url
これは私がこれまでに持っている PHP コードです.PHP で CSV の正しい構造を実現し、必要な正確な方法で行を追加する方法を理解する必要があります..
ご協力いただきありがとうございます!
function genCSV($filename, $attachment = true, $headers = true) {
// send response headers to the browser
header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename=' . $filename);
$fp = fopen('php://output', 'w');
$query = "SELECT * FROM campaigns";
$result = mysql_query($query) or die(mysql_error());
if ($headers) {
// output header row (if at least one row exists)
$row = mysql_fetch_assoc($result);
if ($row) {
fputcsv($fp, array_keys($row));
// reset pointer back to beginning
mysql_data_seek($result, 0);
}
}
while ($row = mysql_fetch_assoc($result)) {
fputcsv($fp, $row);
}
fclose($fp);
}