[英]php mysql exit if query is empty
我有一個運行MySQL查詢的PHP腳本,並將結果輸出到CSV文件並通過電子郵件發送該文件。 如果MySQL查詢返回null,我試圖弄清楚如何在發送電子郵件之前退出腳本
<?php
function create_csv_string($data) {
mysql_connect("mysql.com:23306","root","20131021");
$data = mysql_query(" Select * from datbases");
// Open temp file pointer
if (!$fp = fopen('php://temp', 'w+')) return FALSE;
fputcsv($fp, array('table1','table12', 'table12','table14'));
// Loop data and write to file pointer
while ($line = mysql_fetch_assoc($data)) fputcsv($fp, $line);
// Place stream pointer at beginning
rewind($fp);
// Return the data
return stream_get_contents($fp);
}
function send_csv_mail($csvData, $body, $to = 'data@gmail.com',$subject = 'Report', $from = 'noreply@me.com') {
// This will provide plenty adequate entropy
$multipartSep = '-----'.md5(time()).'-----';
// Arrays are much more readable $headers = array(
"From: $from",
"Reply-To: $from",
"Content-Type: multipart/mixed; boundary=\"$multipartSep\""
);
// Make the attachment
$attachment = chunk_split(base64_encode(create_csv_string($csvData)));
// Make the body of the message
$body = "--$multipartSep\r\n"
"Content-Type: text/plain; charset=ISO-8859-1; format=flowed\r\n"
. "Content-Transfer-Encoding: 7bit\r\n"
. "\r\n"
. "$body\r\n"
. "--$multipartSep\r\n"
. "Content-Type: text/csv\r\n"
. "Content-Transfer-Encoding: base64\r\n"
. "Content-Disposition: attachment; filename=\"-Report-" . date("F-j-Y") . ".csv\"\r\n"
. "\r\n"
. "$attachment\r\n"
. "--$multipartSep--";
// Send the email, return the result
return @mail($to, $subject, $body, implode("\r\n", $headers));
}
$array = array(array(1,2,3,4,5,6,7), array(1,2,3,4,5,6,7), array(1,2,3,4,5,6,7));
send_csv_mail($array, "Report \r\n \r\n ");
?>
在mysql_query
行之后添加此
if (mysql_num_rows($data) == 0)
return(0);
而在其他代碼中
if ($attachment == 0)
die(0);
檢查mysql_num_rows是否返回任何行,否則返回false,並檢查在何處調用該方法。
像這樣:
<?php
function create_csv_string($data) {
mysql_connect("mysql.com:23306","root","20131021");
$data = mysql_query(" Select * from datbases");
if (mysql_num_rows($data) === 0) return false;
// Open temp file pointer
if (!$fp = fopen('php://temp', 'w+')) return FALSE;
fputcsv($fp, array('table1','table12', 'table12','table14'));
// Loop data and write to file pointer
while ($line = mysql_fetch_assoc($data)) fputcsv($fp, $line);
// Place stream pointer at beginning
rewind($fp);
// Return the data
return stream_get_contents($fp);
}
function send_csv_mail($csvData, $body, $to = 'data@gmail.com',$subject = 'Report', $from = 'noreply@me.com') {
// This will provide plenty adequate entropy
$multipartSep = '-----'.md5(time()).'-----';
// Arrays are much more readable $headers = array(
"From: $from",
"Reply-To: $from",
"Content-Type: multipart/mixed; boundary=\"$multipartSep\""
);
// Make the attachment
$attachment = create_csv_string($csvData);
if ($attachment === false) die('No rows found');
$attachment = chunk_split(base64_encode($attachment));
// Make the body of the message
$body = "--$multipartSep\r\n"
"Content-Type: text/plain; charset=ISO-8859-1; format=flowed\r\n"
. "Content-Transfer-Encoding: 7bit\r\n"
. "\r\n"
. "$body\r\n"
. "--$multipartSep\r\n"
. "Content-Type: text/csv\r\n"
. "Content-Transfer-Encoding: base64\r\n"
. "Content-Disposition: attachment; filename=\"-Report-" . date("F-j-Y") . ".csv\"\r\n"
. "\r\n"
. "$attachment\r\n"
. "--$multipartSep--";
// Send the email, return the result
return @mail($to, $subject, $body, implode("\r\n", $headers));
}
$array = array(array(1,2,3,4,5,6,7), array(1,2,3,4,5,6,7), array(1,2,3,4,5,6,7));
send_csv_mail($array, "Report \r\n \r\n ");
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.