Hello and welcome to our community! Is this your first visit?
Register
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
    Posts
    2
    Thanks
    0
    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)

    <?php
    // Database Connection

    $host="localhost";
    $uname="root";
    $pass="";
    $database = "a2zwebhelp";

    $connection=mysql_connect($host,$uname,$pass);

    echo mysql_error();

    //or die("Database Connection Failed");
    $selectdb=mysql_select_db($database) or
    die("Database could not be selected");
    $result=mysql_select_db($database)
    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;
    exit;

    ?>


    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">
    </form>



    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
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,865
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    Code that i am using is (page name-export.php)
    <form action="export.php" method="post">
    Any clues?
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,474
    Thanks
    63
    Thanked 537 Times in 524 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.
    I can't really think of anything to write here now...


  •  

    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
    •