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

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4434
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post 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...";
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6425
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post 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']
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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.
Post Reply