【问题标题】:PHP script export new entries to SQL table and overwrite filePHP脚本将新条目导出到SQL表并覆盖文件
【发布时间】:2013-02-08 03:09:42
【问题描述】:

我正在尝试创建一个 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);

?>

【问题讨论】:

  • 你有什么问题?
  • 它不会覆盖 csv 目录中的文件。我只需要选择那天的新条目。
  • 为了让它工作,我添加了一些脚本来清空目录,然后再运行脚本的其余部分。这就是问题的一部分。

标签: php sql pdo cron cron-task


【解决方案1】:

你的 sql 必须:

SELECT * FROM enquiries where DATE(DateField) = CURDATE() ORDER BY firstname

【讨论】:

    猜你喜欢
    • 2014-01-19
    • 1970-01-01
    • 2017-10-16
    • 2013-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-03
    • 1970-01-01
    相关资源
    最近更新 更多