我在让 postgres 查询在我正在运行的某些 php 中工作时遇到了一些麻烦。本质上,我正在创建一份每日报告,该报告将通过电子邮件发送到分发列表,但我无法将我的数据提取到 csv 中。这是我以前没有做过的事情,所以我一直在使用另一个类似的报告来做到这一点。有人可以帮忙吗?
include('/some_connection/db_connect.inc');
ob_flush();
$yesterday_db = date('Y-m-d', strtotime('-1 days'));
$yesterday_msg = date('m/d/Y', strtotime('-1 days'));
$yesterday_fn = date('Ymd', strtotime('-1 days'));
$filename = 'fileData_'.$yesterday_fn.'.csv';
$path = '/tmp/';
$res = $db->getAll("SELECT tickets.order_number, tickets.customer_id, tickets.ticket_id, customers.email, (customers.first_name || ' ' || customers.last_name), customers.address_line_1, customers.address_line_2, customers.city, customers.state, customers.zip, (customers.first_name || ' ' || customers.last_name), customers.address_line_1, customers.address_line_2, customers.city, customers.state, customers.zip, customers.phone_1, customers.phone_2, customers.phone_3, contacts.notes
FROM tickets, customers, contacts
WHERE tickets.initial_call_date >= '$yesterday_db'
AND tickets.initial_call_date <= '$yesterday_db'
AND tickets.customer_id = customers.customer_id
AND tickets.ticket_id = contacts.ticket_id");
$db2 = DB::connect('database');
$db2->setFetchMode(DB_FETCHMODE_ASSOC);
$handle = fopen($path.$filename,'w');
if(DB::isError($res)) { echo $res->getdebuginfo(); return;}
fwrite($handle,'"Account","Customer Code","Email","Bill Name","Bill Address 1","Bill Address 2","Bill City","Bill State","Bill Zip","Ship Name","Ship Address 1","Ship Address 2","Ship City","Ship State","Ship Zip","Phone 1","Phone 2","Phone 3","Description"'."\n");
foreach($res as $crow){
$id = $crow['customer_id'];
$res = $db->query("SELECT tickets.order_number, tickets.customer_id, tickets.ticket_id, customers.email, (customers.first_name || ' ' || customers.last_name) as bill_name, customers.address_line_1, customers.address_line_2, customers.city, customers.state, customers.zip, (customers.first_name || ' ' || customers.last_name) as ship_name, customers.address_line_1, customers.address_line_2, customers.city, customers.state, customers.zip, customers.phone_1, customers.phone_2, customers.phone_3, contacts.notes
FROM tickets, customers, contacts
WHERE tickets.initial_call_date >= '$yesterday_db'
AND tickets.initial_call_date <= '$yesterday_db'
AND tickets.customer_id = customers.customer_id
AND tickets.ticket_id = contacts.ticket_id");
if(DB::isError($res)) { echo 'Error getting information for '.$id.'.'.$res->getdebuginfo()."\n"; return; }
$row = $res->fetchrow();
if(!empty($row)){
fwrite($handle,'"","'.$row['order_number'].'","'.$row['customer_id'].'","'.$row['ticket_id'].'","'.$row['email'].'","'.$row['bill_name'].'","'.$row['address_line_1'].'","'.$row['address_line_2'].'","'.$row['city'].'","'.$row['state'].'","'.$row['zip'].'","'.$row['ship_name'].'","'.$row['address_line_1'].'","'.$row['address_line_2'].'","'.$row['city'].'","'.$row['state'].'","'.$row['zip'].'","'.$row['phone_1'].'","'.$row['phone_2'].'","'.$row['phone_3'].'","'.$row['notes']."\n");
}
}
fclose($handle);
$to = 'someone@somemail.com';
$from = 'system@somemail.com';
$from_name = 'Daily Upload';
$replyto = '';
$subject = 'CSV for '.$yesterday_msg."\n";
$message = 'Attached is the ticket data for '.$yesterday_msg."\n";
mail_attachment($filename,$path,$to,$from,$from_name,$replyto,$subject,$message);
function remove_non_numeric($string) {
return preg_replace('/\D/', '', $string);
}
function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
$file = $path.$filename;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$name = basename($file);
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: text/csv; name=\"".$filename."\"\r\n"; // use different content types here
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--".$uid."--";
if (mail($mailto, $subject, "", $header)) {
echo "mail send ... OK"; // or use booleans here
} else {
echo "mail send ... ERROR!";
}
}?>