php mysql退出,如果查询为空

时间:2017-01-03 02:20:37

标签: php mysql

我有一个运行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 ");
   ?>

3 个答案:

答案 0 :(得分:0)

T行之后添加此

mysql_query

在其他代码中

 if (mysql_num_rows($data) == 0)
      return(0);

答案 1 :(得分:0)

其他人已经比我更好地解释了这一点。

使用

检查空结果

&#13;
&#13;
weird_variable
&#13;
&#13;
&#13;

Displaying a message when resultset is empty

答案 2 :(得分: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 ");

?>