Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP脚本将新条目导出到SQL表并覆盖文件_Php_Sql_Pdo_Cron_Cron Task - Fatal编程技术网

PHP脚本将新条目导出到SQL表并覆盖文件

PHP脚本将新条目导出到SQL表并覆盖文件,php,sql,pdo,cron,cron-task,Php,Sql,Pdo,Cron,Cron Task,我正在尝试创建一个php脚本,它将由cron作业每晚运行 数据库保存联系人表单中的条目 其想法是运行php脚本(通过cron),并让它将当天数据库中的任何新条目导出到服务器上的csv文件中,每次都覆盖该文件以进行管理。它只需要从那天起将条目导出到数据库 以下是我目前的情况: <?php // Connect and query the database for the users $conn = new PDO("mysql:host=localhost;dbname=dbname",

我正在尝试创建一个php脚本,它将由cron作业每晚运行

数据库保存联系人表单中的条目

其想法是运行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);

?>

您的sql必须:

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

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