夜間に cron ジョブによって実行される php スクリプトを作成しようとしています。
データベースには、連絡先フォームからのエントリが保持されます。
アイデアは、PHPスクリプトを(cron経由で)実行し、その日のデータベースからサーバー上のcsvファイルに新しいエントリをエクスポートし、ハウスキーピングのために毎回そのファイルを上書きすることです. その日のエントリのみをデータベースにエクスポートする必要があります。
現時点で私が持っているものは次のとおりです。
<?php
// Connect and query the database for the users
$conn = new PDO("mysql:host=localhost;dbname=dbname", 'username', 'password');
$sql = "SELECT * FROM enquiries ORDER BY firstname";
$results = $conn->query($sql);
// Pick a filename and destination directory for the file
// Remember that the folder where you want to write the file has to be writable
//$filename = "/tmp/db_user_export_".time().".csv";
$filename = "/home/domain/public_html/csv/db_user_export.csv";
// Actually create the file
// The w+ parameter will wipe out and overwrite any existing file with the same name
$handle = fopen($filename, 'wb');
// Write the spreadsheet column titles / labels
fputcsv($handle, array('FirstName','Email'));
// Write all the user records to the spreadsheet
foreach($results as $row)
{
fputcsv($handle, array($row['firstname'], $row['email']));
}
// Finish writing the file
fclose($handle);
?>