使用来自数据库的查询从Php导出CSV文件

时间:2014-03-14 14:23:15

标签: php mysql

我使用以下代码导出CSV文件

我有一个非常奇怪的问题。 我的$ newQuery给了我(当我回音时)

SELECT * FROM quotes WHERE storeEmail = 'xxxxxxx ';

如果我在phpmyadmin中使用上面的查询,我至少会获得1000行。但是在同一个代码中,它返回null。

$ quotes只是空的。 这里有什么问题

<?php session_start(); 
require_once("common/commonfiles.php");

$menuItem="stores";
$title="Stores";
if (isset($_GET['name']))
{
    $name = trim($_GET['name']);
}

$filename = "export.csv";   
$delimiter= ",";

    // open raw memory as file so no temp files needed, you might run out of memory though
    $f = fopen('php://memory', 'w'); 
    // loop over the (input array
    fputcsv($f, array("Call Details"), "\r\n\r\n"); 
    fputcsv($f, array("UserId","Call Date","Firstname","Lastname","Phone","Zip","Email"), ","); 

    $query = "SELECT callDetails.* , clients.* FROM callDetails JOIN clients ON clients.id = callDetails.userId WHERE storeName = '" . $name ."' ";       
    db::open();

    $result = mysql_query($query) or die(mysql_error());
    $records = Array();

    if(!mysql_num_rows($result)==0)
    {
    $i=0;
    while ($row = mysql_fetch_array($result))
        {
            $records[$i] = $row;
            $i++;
    }
    }
    else
    {
        $records = false;
    }

    db::close();
    if($records)
    {
        foreach ($records as $line) 
        { 
         $newArray = array($line['userId'],
            $line['creationDate'],
            $line['firstname'],
            $line['lastname'],
            $line['phone'],
            $line['zip'],
            $line['email']
          );
        // generate csv lines from the inner arrays
         fputcsv($f, $newArray, $delimiter); 
        }
    }

    $query = "SELECT * FROM stores WHERE name LIKE '%".$name."%' LIMIT 1"; 
    db::open();
        $result = mysql_query($query) or die(mysql_error());
        if(!mysql_num_rows($result)==0){
            $recordset = mysql_fetch_assoc($result);
            }else{
                $recordset = false;
            }
        db::close();
    $recordset = db::getRecord($query);

    $storeEmail = $recordset['email'];

    fputcsv($f, array(""), "\r\n\r\n"); 
    fputcsv($f, array(""), "\r\n\r\n"); 
    fputcsv($f, array(""), "\r\n\r\n"); 

    fputcsv($f, array("Quote Details"), "\r\n\r\n"); 
    fputcsv($f, array("Ring","Quote Time","Firstname","Lastname","Phone","Zip","Email"), ","); 

    $newQuery = "SELECT * FROM quotes WHERE storeEmail = '" . $storeEmail ."'";  

    db::open();
    $newResult = mysql_query($newQuery) or die(mysql_error());
    if(!mysql_num_rows($newResult)==0)
    {
    $i=0;
    while ($row = mysql_fetch_array($newResult))
        {
            $quotes[$i] = $row;
            $i++;
    }
    }
    else
    {
        $quotes = false;
    }
    db::close();

    if($quotes)
    {
        foreach ($quotes as $line) 
        { 
            $newArray = array($line['ring'],
              $line['quoteTime'],
              $line['firstname'],
             $line['lastname'],
             $line['phone'],
             $line['zip'],
             $line['email']
         );
        // generate csv lines from the inner arrays
            fputcsv($f, $newArray, $delimiter); 
        }   
    }
    // rewrind the "file" with the csv lines
    fseek($f, 0);
    // tell the browser it's going to be a csv file
    header('Content-Type: application/csv');
    // tell the browser we want to save it instead of displaying it
    header('Content-Disposition: attachement; filename="'.$filename.'";');
    // make php send the generated csv lines to the browser
    fpassthru($f);

    //fclose($fp);
?>

2 个答案:

答案 0 :(得分:0)

你实际上是在进入mysql_num_rows循环吗?如果不是,则$quotes被设置为false。如果您在那里或不在那里,请尝试打印出来。

if(!mysql_num_rows($newResult)==0) {
     print "Made it!";
}
else {
     print "Didn't Make It.  How Sad.";
}

然后你会知道这是不是你的查询。

如果是您的查询,那么它可能在电子邮件部分失败了。 ($storeEmail = $recordset['email'];)尝试打印您的查询,看看是否正确:

$newQuery = "SELECT * FROM quotes WHERE storeEmail = '" . $storeEmail ."'"; 
print $newQuery;

如果不存在,那么获取电子邮件的查询就会出现问题。

答案 1 :(得分:0)

尝试在线使用修剪:

$newQuery = "SELECT * FROM quotes WHERE storeEmail = '" . trim($storeEmail) ."'"; 

也许$ storeEmail变量有一些额外的空白字符。