使用PHP和mysql创建CSV文件

时间:2019-03-18 19:24:42

标签: php mysql csv

我正在尝试通过php和mysql创建一个csv文件。在mysql Workbench中输入mysql语句时,它将生成所需的结果。但是,当我复制同一条语句并将其粘贴到下面的代码中时,我得到的csv没有结果。可能与我正在创建2个临时表,然后在JOIN语句中使用它们有关吗?

<?php

//Our MySQL connection details.
$host = 'xxx.xxx.xxx';
$user = 'xxxx';
$password = 'xxxx';
$database = 'xxxx';

//Connect to MySQL using PDO.
$pdo = new PDO("mysql:host=$host;dbname=$database", $user, $password);

//Create our SQL query.
$sql = "create temporary table pt_pickups SELECT 
    parts_processed.reason AS 'PO',
    CONCAT('PU-',
            parts_processed.confirmation_number) AS 'INVOICE',
    elite_routes.source AS 'SOURCE',
    elite_routes.source_udid AS 'SOURCE UDID',
    parts_processed.customer_name AS 'DESTINATION',
    parts_processed.customer_number AS 'DESTINATION UDID',
    namefile.address AS 'DESTINATION_ADDRESS',
    namefile.city AS 'DESTINATION_CITY',
    namefile.state AS 'DESTINATION_STATE',
    namefile.zip AS 'DESTINATION_ZIP',
    elite_routes.source AS 'ORDER BY',
    parts_processed.route AS 'ZONE',
    parts_processed.part_number AS 'PART NUMBER',
    parts_processed.description AS 'PART DESCRIPTION',
    parts_processed.ship_to,
    parts_processed.ship_to_address,
    parts_processed.ship_to_city,
    parts_processed.ship_to_state,
    parts_processed.ship_to_zip
FROM
    parts_processed,
    namefile,
    elite_routes,
    data
WHERE
    parts_processed.customer_number = namefile.customernumber
        AND parts_processed.invoice = data.InvoiceNumber
        and elite_routes.route = parts_processed.route
        AND parts_processed.approved = 'APPROVED'
        AND date(parts_processed.date_of_processing) = date(now())
        and (parts_processed.reason LIKE '%CORE%'
        OR parts_processed.reason LIKE '%DAMAGE%'
        OR parts_processed.reason LIKE'%WARRANTY%')
        AND data.PartDescription NOT IN ('CORE RETURN' , 'CLAIM')
        AND data.PartDescription NOT LIKE ('%turbo%')
        AND data.PartDescription NOT LIKE ('%link%')
        AND data.PartDescription NOT LIKE ('%NAVI%')
        AND data.PartDescription NOT LIKE ('%BUMPER%')
        AND data.PartDescription NOT LIKE ('%LEG%')
        AND data.PartDescription NOT LIKE ('%WARRANTY REPLACEMENT PART%')
        AND data.PartDescription NOT LIKE ('%LAMP%')
        AND data.PartDescription NOT LIKE ('%STRUT%')
        AND data.PartDescription NOT LIKE ('%BODY%')
        AND data.PartDescription NOT LIKE ('%PRESSURE%')
        AND data.PartDescription NOT LIKE ('%TUBE%')
        AND data.PartDescription NOT LIKE ('%wheel%')
        AND data.PartDescription NOT LIKE ('%converter%')
        AND ((data.Source IN (9 , 409,
        900,
        901,
        902,
        903,
        904,
        818,
        132,
        232,
        331))
        OR (data.mfg IN ('VW' , 'BW',
        'HA',
        'HV',
        'IS',
        'KM',
        'TL',
        'MD',
        'MB',
        'MI',
        'DA',
        'SA',
        'SU',
        'ZC',
        'TO')
        AND (data.IndCost > 600.00 OR data.indcost < - 600.00)
        AND data.CORE <> 0));
create temporary table reg_pickups SELECT 
    parts_processed.reason AS 'PO',
    CONCAT('PU-',
            parts_processed.confirmation_number) AS 'INVOICE',
    elite_routes.source AS 'SOURCE',
    elite_routes.source_udid AS 'SOURCE UDID',
    parts_processed.customer_name AS 'DESTINATION',
    parts_processed.customer_number AS 'DESTINATION UDID',
    namefile.address AS 'DESTINATION_ADDRESS',
    namefile.city AS 'DESTINATION_CITY',
    namefile.state AS 'DESTINATION_STATE',
    namefile.zip AS 'DESTINATION_ZIP',
    elite_routes.source AS 'ORDER BY',
    parts_processed.route AS 'ZONE',
    parts_processed.part_number AS 'PART NUMBER',
    parts_processed.description AS 'PART DESCRIPTION',
    parts_processed.ship_to,
    parts_processed.ship_to_address,
    parts_processed.ship_to_city,
    parts_processed.ship_to_state,
    parts_processed.ship_to_zip
FROM
    parts_processed,
    namefile,
    elite_routes
WHERE
    parts_processed.customer_number = namefile.customernumber
        and elite_routes.route = parts_processed.route
        AND parts_processed.approved = 'APPROVED'
        AND date(parts_processed.date_of_processing) = date(now())
        and (parts_processed.reason LIKE '%CORE%'
        OR parts_processed.reason LIKE '%DAMAGE%'
        OR parts_processed.reason LIKE'%WARRANTY%');
SELECT 
    *
FROM
    reg_pickups
        LEFT JOIN
    pt_pickups ON reg_pickups.invoice = pt_pickups.invoice
WHERE
    pt_pickups.invoice IS NULL;
";

//Prepare our SQL query.
$statement = $pdo->prepare($sql);

//Executre our SQL query.
$statement->execute();

//Fetch all of the rows from our MySQL table.
$rows = $statement->fetchAll(PDO::FETCH_ASSOC);

//Get the column names.
$columnNames = array();
if(!empty($rows)){
    //We only need to loop through the first row of our result
    //in order to collate the column names.
    $firstRow = $rows[0];
    foreach($firstRow as $colName => $val){
        $columnNames[] = $colName;
    }
}

date_default_timezone_set('America/New_York');

//Setup the filename that our CSV will have when it is downloaded.
$fileName = "U:\ElitePickUps\Pickups-".date('m-d-y').".csv";

//Set the Content-Type and Content-Disposition headers to force the download.
header('Content-Type: application/excel');
header('Content-Disposition: attachment; filename="' . $fileName . '"');

//Open up a file pointer
$fp = fopen($fileName, 'w');

//Start off by writing the column names to the file.
fputcsv($fp, $columnNames);

//Then, loop through the rows and write them to the CSV file.
foreach ($rows as $row) {
    fputcsv($fp, $row);
}

//Close the file pointer.
fclose($fp);

0 个答案:

没有答案