【发布时间】:2015-06-26 16:04:30
【问题描述】:
我收到的错误消息是标题。我正在使用 mysqli_ 我在空白页上使用 mysql_ 来测试它。当我把它放到我真实页面上的点击中时,它告诉我我不能使用 mySql_,因为它太旧了,所以将它转换为 mySqli_。我现在收到的错误消息是:
“警告:无法修改标头信息 - 标头已由 (输出开始于 /xx/xxx/xxx.php:183)在 /xxx/xxx/xxx.php 在线 232 警告:无法修改标头信息 - 标头已发送 通过(输出开始于 /xxx/xxx/xxx.php:183)在 /xxx/xxx/xxx.php 上 第 233 行致命错误:调用未定义的函数 outputcsv() /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;
}
【问题讨论】:
-
嗨,科马克。你见过这个问题吗?这可能会有所帮助。 stackoverflow.com/questions/13108157/php-array-to-csv