Jump to content

Recommended Posts

Hi Everyone

 

I'm pretty new to PHP and I have worked out how to do a CSV export from my contacts database which works perfectly.

 

but I need to export a pipe delimited csv, which im really struggling with.

 

This is how I need my csv to look like

 

1234567890|surname|forename|email@somewhere.com|test-contact|1|address|city|state|US|unit|

 

I just need help with the echoing out this file right.

 

I have a while loop that grabs the data like so

$data_string .= 
"\"" . $row['number'] ."\"
 "\"\n";

of course this only does the number but im sure you get the jist.

 

any help given I thank in advance.

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/297711-export-csv-from-mysql-pipe-delimited/
Share on other sites

If you want to populate your csv file with the results from your query then use fputcsv. Example (I'm guessing your table column names)

// path to csv file to write data to
$data_file = 'myfile.csv';

$result = $mysqli->query('SELECT number, surname, forename, email, message, id, address, city, state, country, unit FROM table');

// open/create the csv file
$handle = fopen($data_file, 'w');

// loop over results from query
while($row = $result->fetch_row())
{
    // write row contents to csv file
    fputcsv($handle, $row);
}
fclose($handle);

Documentation

http://php.net/fputcsv

 

Alternatively if your mysql user has file privileges, you can have the actual query dump the data into the csv file using a SELECT INTO query

okay, so I have got the fputcsv working, and it is pulling out what i want and i can easily manipulate it now, but the headers give me an excel file with the webpage echo's

When all I need is the CSV file that was created, If I FTP onto the server I can see the csv file with the correct fields

// path to csv file to write data to
$data_file = 'myNewfile.csv';

header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename='.$data_file);

$result = $db_mysqli->query('SELECT * FROM newfies');

// open/create the csv file
$handle = fopen($data_file, 'w');

// loop over results from query
while($row = $result->fetch_row())
{
    // write row contents to csv file
    fputcsv($handle, $row, '|');
}
fclose($handle);
}

Wow, I think im going insane, I've tried that with no luck :( is doesn't create a csv at all and doesn't download one.

 

here's my code.

$sql = 'SELECT * FROM newfies';

function sql2csv($db_mysqli, $sql, $filename='myNewfile.csv', $headings=0)

{
    if (!$filename)
        $f = 'download_' . date('ymdhi') . '.csv';
    else 
         $f = $filename;
    $fp = fopen('php://output', 'w');        // so you can fputcsv to STDOUT
    if ($fp) {
        $res = $db_mysqli->query($sql);
        if ($res) {
            header('Content-Type: text/csv');
            header('Content-Disposition: attachment; filename="'.$f.'"');
            header('Pragma: no-cache');
            header('Expires: 0');
            $row = $res->fetch_assoc();
            if ($headings) {
                fputcsv($fp, array_keys($row),'|');
            }
            do {
                fputcsv($fp, $row);
            } while ($row = $res->fetch_assoc());
            
        }
        else echo "Error in query";
        fclose($fp);
    }
    
}

}

Hi I've for the function to work now but it is still inserting the html code within the php pages

 

below is the end of the script which calls the function

 

include('test.php');

sql2csv($db_mysqli, $sql, $filename='', $headings=0);

}
?>

and the function is now its own page named test.php

File 1

<a href="file2.php">Download newfies</a>

File2.php (the download code goes in a file of its own)

<?php

$sql = 'SELECT * FROM newfies';
sql2csv($db_mysqli, $sql, $filename='myNewfile.csv', $headings=0);


function sql2csv($db_mysqli, $sql, $filename='', $headings=0)

{
if (!$filename)
    $f = 'download_' . date('ymdhi') . '.csv';
else
    $f = $filename;
$fp = fopen('php://output', 'w'); // so you can fputcsv to STDOUT
if ($fp) {
    $res = $db_mysqli->query($sql);
    if ($res) {
      header('Content-Type: text/csv');
      header('Content-Disposition: attachment; filename="'.$f.'"');
      header('Pragma: no-cache');
      header('Expires: 0');
      $row = $res->fetch_assoc();
    if ($headings) {
      fputcsv($fp, array_keys($row),'|');
}
do {
    fputcsv($fp, $row);
} while ($row = $res->fetch_assoc());

}
else echo "Error in query";
fclose($fp);
}

}



?>
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.