我在使用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!";
}
}?>
答案 0 :(得分:0)
问题可能出在你的if:
上if(empty($row)){
//Write here
}
应该是:
if(!empty($row)){
//Write here
}
另请注意,fputcsv在这种情况下非常有用。
编辑:另一个问题是你的循环:
foreach($res as $crow){
$id = $crow['customer_id'];
$res = $db->query(query);
}
请注意,在第一个循环之后,您正在使用$res
变量。
答案 1 :(得分:0)
我发布了最终代码,以便在需要时帮助其他人。
<?php
include('/home/ ... some connection ... /db_connect.inc');
ini_set("auto_detect_line_endings", true);
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'));
$today_db = date('Y-m-d', strtotime('today'));
$qty = 1;
$amt = 0;
$status = "'Service'";
$filename = 'dispatchTrack_'.$yesterday_fn.'.csv';
$path = '/tmp/';
$db2 = DB::connect('odbc://connection');
$db2->setFetchMode(DB_FETCHMODE_ASSOC);
if (!$db2) {
die('Could not connect: ' . pg_last_error());
}
$handle = fopen($path.$filename,'w');
$query = "SELECT distinct (wo.work_order_id || '-' || t.order_number) AS order_number, wo.customer,
(c.first_name || ' ' || c.last_name) as bill_name,
(c.address_line_1) as bill_address_1, (c.address_line_2) as bill_address_2,
(c.city) as bill_city, (c.state) as bill_state, (c.zip) as bill_zip,
(c.first_name || ' ' || c.last_name) as ship_name,
(c.address_line_1) as ship_address_1, (c.address_line_2) as ship_address_2,
(c.city) as ship_city, (c.state) as ship_state, (c.zip) as ship_zip,
(c.phone_1) as phone1, (c.phone_2) as phone2, (c.phone_3) as phone3,
c.email, (p.item_id) AS model, (ppi.display_name || ' - ' || p.item_description) AS description, $qty, $amt, $status, (wo.visit_date) AS delivery_date,
(techs.dispatch_code) AS truck, (p.problem_description) AS order_detail
FROM work_orders AS wo, customers AS c, tickets AS t, techs, problems AS p
JOIN pta_package_items AS ppi
ON p.item_id = ppi.item_id
WHERE c.customer_id IN (SELECT DISTINCT wo.customer
FROM work_orders AS wo
WHERE wo.date_created >= '$yesterday_db'
AND wo.date_created < '$today_db')
AND wo.customer = c.customer_id
AND t.ticket_id = wo.ticket_id
AND p.ticket_id = t.ticket_id
AND techs.tech_id = wo.tech
AND wo.status = 1
ORDER BY order_number";
$result = pg_query($query) or die ('Query failed: ' . pg_last_error());
fputcsv($handle, array('Order Number','Customer Code','Bill Name','Bill Address1','Bill Address2','Bill City','Bill State','Bill Zip','Ship Name','Ship Address1','Ship Address2','Ship City','Ship State','Ship Zip','Phone1','Phone2','Phone3','Email','Model','Description','Quantity','Amount','Delivery Type','Delivery Date','Truck','Order Detail','Comment1','Comment2','Comment3'));
while ($row = pg_fetch_row($result)) fputcsv($handle, $row, $delimeter = ',', $enclosure = '"');
fclose($handle);
...文件的其余部分与原来的相同。再次感谢那些评论我非常感谢您的帮助。