我已经使用笔记本电脑成功运行了这个PHP代码,但是当我在服务器上试用它时失败了。
此文件的目的是将表(SQL)下载为.xls格式。
<?php
session_start();
// Set name of excel doc
if(isset($_POST['t'])){
$setExcelName = $_POST['t'];
}else{
$setExcelName = 'Semua permohonan';
}
if(isset($_SESSION['demo']) AND $_SESSION['demo'] = 'yes'){
$demo = ' (DEMO)';
}else{
$demo = '';
}
include'connect.php';
// Define header / field name
$sql = "SHOW COLUMNS FROM senaraipermohonan";
$result = $conn->query($sql);
$setMainHeader = '';
while($row = $result->fetch_assoc()){
$rowname = '';
$rowname = '"'.$row['Field'].'"';
$setMainHeader .= $rowname . "\t";
}
// Set record in table
if(isset($_POST['q'])){
$sql = $_POST['q'];
}else{
$sql = "SELECT * FROM senaraipermohonan";
}
$record = $conn->query($sql);
if($record->num_rows >0){
$setData = '';
while ($row = $record->fetch_row()) {
$rowData = '';
foreach ($row as $value) {
$value = '"' . $value . '"' . "\t";
$rowData .= $value;
}
$setData .= $rowData . "\n";
}
}else{
$setData = "Tiada data";
}
//This Header is used to make data download instead of display the data
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=Permohonan SMAN2017 - " . $setExcelName . $demo . ".xls");
header("Pragma: no-cache");
header("Expires: 0");
//It will print all the Table row as Excel file row with selected column name as header.
echo ucwords($setMainHeader)."\n".$setData."\n";
?>
它返回:
Not Acceptable
An appropriate representation of the requested resource /download_as_excel.php could not be found on this server.
Additionally, a 406 Not Acceptable error was encountered while trying to use an ErrorDocument to handle the request.
这个问题的可能原因是什么?谢谢。
答案 0 :(得分:0)
看起来在处理错误时出现错误。
所以你有两件事要做:
对于第二个,我会寻找数据库凭据(用户名,密码等),我希望那里有错误。我认为您的笔记本电脑和服务器上的用户名,密码和数据库名称不同。