Jump to content

mysql putcsv issue.


synking

Recommended Posts

Hey guys,  So i recently was trying to get mysql generated csv file.... found that to hard to get right through pdo.

 

So what i did instead is had php parse the data and use fputcsv but it's not working for me quite right thought i would see what im doing wrong.

 

<?php

include('inc/dbconnect.php');

$filename = 'Innovation_data.csv';

try {

   $dbh = new pdo("mysql:host=$serv;dbname=$data", $name, $pass);
   $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

   $sql = "SELECT *
           FROM Customer_Card";

   $sth = $dbh->prepare($sql);

   $sth->execute();

   $filename = "/var/www/html/innovation/inc/crad".time().".csv";

   $handle = fopen($filename, 'w+');

   foreach($sth->fetch(PDO::FETCH_ASSOC) as $row => $value) {
           fputcsv($handle, array($row, $value));
   }

   fclose($handle);

$dbh=null;

} catch(PDOException $e) {
     echo ("I'm sorry, Dave. I'm afraid I can't do that.");
     error_log($e->getMessage());
}


header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Length: ". filesize("$filename").";");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Type: application/octet-stream; ");
header("Content-Transfer-Encoding: binary");

readfile($filename);

?>

 

I get the file created and downloaded but when i open it it's only the first line of the mysql table.  And the name of the file is the whole sting /var/ww.... which i could correct by just fixing the string but i am just confused as why it's not writing the enitre table to the database.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.