gpt4 book ai didi

PHP linux crontab 导出mysql数据到Excel

转载 作者:行者123 更新时间:2023-11-29 09:49:08 34 4
gpt4 key购买 nike

所以,我有这段代码将数据从mysql数据库导出到excel文件,我希望它与crobjob一起运行,问题是这段代码没有提取任何值,它只是给了我一个空的scrren,我不知道不知道为什么...

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

关于PHP linux crontab 导出mysql数据到Excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55148738/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com