簡體   English   中英

PHP將mysql數據導出到Excel crontab linux

[英]PHP export mysql data to Excel crontab linux

所以,我有這段代碼可以將數據從mysql數據庫導出到excel文件,並且我希望它與crobjob一起運行,問題是這段代碼沒有提取任何值,只是給了我一個我不知道的空SCRRREN為什么...

<?php  
//export.php  
$connect = mysqli_connect("localhost", "user", "pw", "bd");
$output = '';
if(isset($_POST["export"]))
{
    $query = "SELECT * FROM PersonalData ";
    $result = mysqli_query($connect, $query);
    if(mysqli_num_rows($result) > 0)
    {
        $output .= '
            <table class="table" bordered="1">  
                    <tr align="center">  
                        <th align="center">Family Name</th>  
                        <th align="center">First Name</th>  
                    </tr>';
        while($row = mysqli_fetch_array($result))
        {
            $output .= '
                    <tr>  
                       <td align="center">'.$row["FamilyName"].'</td>  
                       <td align="center">'.$row["FirstName"].'</td>  
                    </tr>';
        }
        $output .= '</table>';
        header('Content-Type: application/xls');
        header("Content-Type: application/force-download");
        header('Content-Disposition: attachment; filename=assets_colaboradores.xls');
        //
        echo $output;
    }
}
?>

嘗試輸出為CSV。 這是一個將關聯數組轉換為我構建的CSV的函數:

function array2CSV($fileName, $array, $headers = false) {

    // Set file headers
    header('Content-type: text/csv');
    header('Content-Disposition: attachment; filename="' . $fileName . '"');
    header('Pragma: no-cache');
    header('Expires: 0');

    // Open CSV to write
    $file = fopen('php://output', 'w');

    // Assign header names either from $headers or the array keys of $array
    $headers = ($headers) ? $headers : array_keys($array[0]);

    // Write the header row into the file
    fputcsv($file, $headers);
    foreach ($array as $row) {
        // Write a row to the file
        fputcsv($file, $row);
    }
    exit();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM