Page 1 of 1

Why is this CSV extract script failing on $row[email]?

Posted: Thu Apr 28, 2016 5:48 am
by simonmlewis
I have this script that
I get this error from the script below:

[text]Notice: Undefined variable: csv_output in C:\xampp\phpMyAdmin\site-2016\csv_vs.php on line 14

Notice: Use of undefined constant email - assumed 'email' in C:\xampp\phpMyAdmin\site-2016\csv_vs.php on line 14[/text]

Then I get loads of the "constant email" script error.

The field is named email. And the other fields are correct.

Code: Select all

$todaydate = date('Y-m-d');
include "dbconn.php";
$result = mysql_query("SELECT email FROM admin WHERE usertype = 'customer'");
while($row = mysql_fetch_array($result)) 
{      
  $csv_output .= '"'.$row[email].'"';
    $csv_output .= "\015\012";
}
  //You cannot have the breaks in the same feed as the content. 
  header("Content-type: application/vnd.ms-excel");
  header("Content-disposition: csv; filename=VS_EmailStore_" . date("Y-m-d") .".csv");
  print $csv_output;
  exit;
       	mysql_close($sqlconn);
  echo "Extract in progress - one moment please...";

Re: Why is this CSV extract script failing on $row[email]?

Posted: Thu Apr 28, 2016 6:17 am
by Celauran
$csv_output isn't declared anywhere, so the first time you try to append to it, PHP is going to complain. The second notice is simply because you're referencing $row[email] rather than the correct $row['email']

Re: Why is this CSV extract script failing on $row[email]?

Posted: Thu Apr 28, 2016 2:51 pm
by requinix
1.

Code: Select all

header("Content-disposition: csv; filename=VS_EmailStore_" . date("Y-m-d") .".csv");
"csv" is not valid for the Content-Disposition. It is either "inline" to show in the browser or "attachment" to force a download.

Code: Select all

header("Content-disposition: attachment; filename=VS_EmailStore_" . date("Y-m-d") .".csv");
2.

Code: Select all

exit;
        mysql_close($sqlconn);
  echo "Extract in progress - one moment please...";
Nothing after the exit; will execute. It's the whole point of that command.

3. You cannot output messages like that as well as do the CSV download. Get rid of the message.