The error message i am getting is with the headers. I am using mysqli_ i used mysql_ on a blank page to test it. When I put it into the click on my real page it told me I couldn't use mySql_ as it was old so converted it to mySqli_. the error message i'm getting now is:
"Warning: Cannot modify header information - headers already sent by (output started at /xx/xxx/xxx.php:183) in /xxx/xxx/xxx.php on line 232 Warning: Cannot modify header information - headers already sent by (output started at /xxx/xxx/xxx.php:183) in /xxx/xxx/xxx.php on line 233 Fatal error: Call to undefined function outputcsv() in /xxx/xxx/xxx.php on line 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;
}