PHP экспорт данных MySQL в Excel crontab Linux - PullRequest
0 голосов
/ 13 марта 2019

Итак, у меня есть этот код для экспорта данных из базы данных MySQL в файл Excel, и я хочу, чтобы он работал с crobjob, проблема в том, что этот код не извлекает никаких значений, он просто дает мне пустой экран не знаю почему ...

<?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;
    }
}
?>

1 Ответ

1 голос
/ 13 марта 2019

Попробуйте вывести в 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();
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...