我有一个数据库,我正在从中下载某些表,例如员工表和办公室表。
我有将其下载到 CSV 文件的按钮,以便可以轻松地将其打开到 Excel 中。
当我在本地机器上时,这段代码效果很好,但是当我将它上传到服务器的那一刻,我收到警告消息,头文件已经由另一个页面发送,然后 SQL 语句将请求的信息转储到页面上。
所以这是我正在使用的代码:
主页按钮:
<form method="get" action="export_myStaff.php">
<button type="submit" class="Mybutton">Download Staff Directory</button>
</form>
export_myStaff.php
require_once('../Connections/myconnections.php');
// Connect to the database
$link = mysql_connect($hostname, $username, $password);
mysql_select_db($database_testSite );
require_once('csv_myStaff.php');
$staff_table="myStaff_tbl";
exportMysqlToCsv($staff_table);
csv_myStaff.php
function exportMysqlToCsv($staff_table) {
$csv_terminated = "\n";
$csv_separator = ",";
$csv_enclosed = '"';
$csv_escaped = "\\";
$sql_query = "SELECT * FROM myStaff_tbl"
// Gets the data from the database
$result = mysql_query($sql_query);
$fields_cnt = mysql_num_fields($result);
$schema_insert = '';
for ($i = 0; $i < $fields_cnt; $i++) {
$l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed,
stripslashes(mysql_field_name($result, $i))) . $csv_enclosed;
$schema_insert .= $l;
$schema_insert .= $csv_separator;
} // end for
$out = trim(substr($schema_insert, 0, -1));
$out .= $csv_terminated;
// Format the data
while ($row = mysql_fetch_array($result)) {
$schema_insert = '';
for ($j = 0; $j < $fields_cnt; $j++) {
if ($row[$j] == '0' || $row[$j] != '') {
if ($csv_enclosed == '') {
$schema_insert .= $row[$j];
} else {
$schema_insert .= $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed,
$row[$j]) . $csv_enclosed;
}
} else {
$schema_insert .= '';
}
if ($j < $fields_cnt - 1) {
$schema_insert .= $csv_separator;
}
} // end for
$out .= $schema_insert;
$out .= $csv_terminated;
} // end while
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Length: " . strlen($out));
// Output to browser with appropriate mime type, you choose ;)
header("Content-type: text/x-csv");
//header("Content-type: text/csv");
//header("Content-type: application/csv");
$filename = $filename . "_" . date("Y-m-d_H-i", time());
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header("Content-disposition: filename=" . $filename . ".csv");
//header("Content-Disposition: attachment; filename=$filename");
echo $out;
exit;
}
任何想法为什么它可以在我的本地主机上工作但不能在服务器上工作?