我得到的错误信息是标题。我正在使用mysqli_我在空白页面上使用了mysql_来测试它。当我把它放到我真实页面上的点击时它告诉我我不能使用mySql_,因为它已经老了所以将它转换为mySqli_。我现在得到的错误信息是:
"警告:无法修改标头信息 - 已发送的标头 (输出从/xx/xxx/xxx.php:183开始)/xxx/xxx/xxx.php在线 232警告:无法修改标头信息 - 已发送的标头 在/xxx/xxx/xxx.php上的(输出从/xxx/xxx/xxx.php:183开始) 第233行致命错误:调用未定义的函数outputcsv()in /xxx/xxx/xxx.php在第237行"
require_once 'dbconfig.php';
$conn = new mysqli("xxxx", "xxxx", $password, $dbname);//host, user, password, database
$result = $conn->query('SELECT * FROM reports') or die(mysqli_error());
//these two lines are the lines 232 and 233
header('Content-Type: text/csv'); // tell the browser to treat file as CSV
header('Content-Disposition: attachment;filename=report.csv'); // tell browser to download a file in user's system with name export.csv
$row = mysqli_fetch_assoc($result); // Get the column names
if ($row) {
outputcsv(array_keys($row)); // It wil pass column names to outputcsv function
}
//this line here is 237
while ($row) {
outputcsv($row); // loop is used to fetch all the rows from table and pass them to outputcsv func
$row = mysqli_fetch_assoc($result);
}
function outputcsv($fields) {
$csv = '';
foreach ($fields as $field) {
$csv .= '"' . $field . '",';
}
$csv .= "\r\n"; //Give a carriage return and new line space after each record
echo $csv;
}
答案 0 :(得分:1)
您需要在配置文件中设置输出缓冲区
或
只需在代码的第一行写下ob_start()..
我还建议以xls格式导出以获得更好的兼容性。
答案 1 :(得分:0)
您需要查找php output buffering。
您正在第183行headers already sent by (output started at /xx/xxx/xxx.php:183)
上回显或发送某种数据,然后修改标题。您可以使用OB缓冲要发送到浏览器的信息,修改标题然后刷新缓冲区。
ob_start();
require_once 'dbconfig.php';
$conn = new mysqli("xxxx", "xxxx", $password, $dbname);//host, user, password, database
$result = $conn->query('SELECT * FROM reports') or die(mysqli_error());
//these two lines are the lines 232 and 233
header('Content-Type: text/csv'); // tell the browser to treat file as CSV
header('Content-Disposition: attachment;filename=report.csv'); // tell browser to download a file in user's system with name export.csv
$row = mysqli_fetch_assoc($result); // Get the column names
if ($row) {
outputcsv(array_keys($row)); // It wil pass column names to outputcsv function
}
//this line here is 237
while ($row) {
outputcsv($row); // loop is used to fetch all the rows from table and pass them to outputcsv func
$row = mysqli_fetch_assoc($result);
}
function outputcsv($fields) {
$csv = '';
foreach ($fields as $field) {
$csv .= '"' . $field . '",';
}
$csv .= "\r\n"; //Give a carriage return and new line space after each record
echo $csv;
}
ob_flush();
您还可以使用ob_get_flush()
或ob_get_contents()
将缓冲区的内容返回给字符串,允许从函数等返回。注意:ob_get_contents()
不清除缓冲区它只是在那时返回内容。
我还看到有关outputcsv($ fields)未定义的错误,但是在解释器可以通过标题问题后可能会清除。