I have a PHP script that runs a MySQL query and outputs the results to a CSV file and emails the file. I'm trying to figure out how to exit the script before sending the email if the MySQL query returns 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
"
"Content-Type: text/plain; charset=ISO-8859-1; format=flowed
"
. "Content-Transfer-Encoding: 7bit
"
. "
"
. "$body
"
. "--$multipartSep
"
. "Content-Type: text/csv
"
. "Content-Transfer-Encoding: base64
"
. "Content-Disposition: attachment; filename=\"-Report-" . date("F-j-Y") . ".csv\"
"
. "
"
. "$attachment
"
. "--$multipartSep--";
// Send the email, return the result
return @mail($to, $subject, $body, implode("
", $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
");
?>
Check if mysql_num_rows returns any rows, if not return false, and check where the method is being called.
Like this:
<?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
"
"Content-Type: text/plain; charset=ISO-8859-1; format=flowed
"
. "Content-Transfer-Encoding: 7bit
"
. "
"
. "$body
"
. "--$multipartSep
"
. "Content-Type: text/csv
"
. "Content-Transfer-Encoding: base64
"
. "Content-Disposition: attachment; filename=\"-Report-" . date("F-j-Y") . ".csv\"
"
. "
"
. "$attachment
"
. "--$multipartSep--";
// Send the email, return the result
return @mail($to, $subject, $body, implode("
", $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
");
?>
after the mysql_query
line add this
if (mysql_num_rows($data) == 0)
return(0);
And in the other code
if ($attachment == 0)
die(0);
Others have explained this better than I can.
Do a check for empty result with
mysql_num_rows()
Displaying a message when resultset is empty
</div>