Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jun 2014
    Thanked 0 Times in 0 Posts

    I want to export data from database to csv format.

    I want to export data from database to csv format.

    Code that i am using is (page name-export.php)

    // Database Connection

    $database = "a2zwebhelp";


    echo mysql_error();

    //or die("Database Connection Failed");
    $selectdb=mysql_select_db($database) or
    die("Database could not be selected");
    or die("database cannot be selected <br>");

    // Fetch Record from Database

    $output = "";
    $table = ""; // Enter Your Table Name
    $sql = mysql_query("select * from $table");
    $columns_total = mysql_num_fields($sql);

    // Get The Field Name

    for ($i = 0; $i < $columns_total; $i++) {
    $heading = mysql_field_name($sql, $i);
    $output .= '"'.$heading.'",';
    $output .="\n";

    // Get Records from the table

    while ($row = mysql_fetch_array($sql)) {
    for ($i = 0; $i < $columns_total; $i++) {
    $output .='"'.$row["$i"].'",';
    $output .="\n";

    // Download the file

    $filename = "myFile.csv";
    header('Content-type: application/csv');
    header('Content-Disposition: attachment; filename='.$filename);

    echo $output;


    It is working perfectly but i want another page that can be showed at user end and he can download the file by clicking on a button.. For this purpose the code that i am using is

    <form action="export.php" method="post">
    Please click here to download:
    <input type="button" name="Download" value="Download">

    I think i have done something wrong because the two pages are not getting linked to each other. Whenever i am clicking on the download button it remains on the same page..
    Can plz someone point out the error.

  2. #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Thanked 2,250 Times in 2,237 Posts
    Code that i am using is (page name-export.php)
    <form action="export.php" method="post">
    Any clues?

  3. #3
    Master Coder
    Join Date
    Feb 2011
    Your Monitor
    Thanked 614 Times in 600 Posts
    If you want to CSV your data then you really should base64 encode it first.

    Why? Because the moment your data (eg someones comments in a guestbook) contains a comma, your import script on another server will fail as it will think that the comma is a seperator instead of being part of the data.

    Incidentally, phpmyadmin can export data in CSV format so that may say you some hassle.
    Quote Originally Posted by deathshadow View Post
    So seriously, loosen up that tie, let out the belt, and try relating to normal people on the street instead of the gentleman's club crowd.


Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts