Back Up database as csv file

Discussion in 'PHP' started by newphpcoder, Jan 26, 2012.

  1. #1
    Hi


    I search for a sample code for back up database as csv file.

    I found this code:

    
    <?php
    $host = 'localhost';
    $user = 'root';
    $pass = '';
    $db = 'payroll';
    $table = 'generate_payroll';
    $file = 'export';
    $link = mysql_connect($host, $user, $pass) or die("Can not connect." . mysql_error());
    mysql_select_db($db) or die("Can not connect.");
    $result = mysql_query("SHOW COLUMNS FROM ".$table."");
    $i = 0;
    if (mysql_num_rows($result) > 0) {
    while ($row = mysql_fetch_assoc($result)) {
    $csv_output .= $row['Field']."; ";
    $i++;
    }
    }
    $csv_output .= "\n";
    $values = mysql_query("SELECT * FROM ".$table."");
    while ($rowr = mysql_fetch_row($values)) {
    for ($j=0;$j<$i;$j++) {
    $csv_output .= $rowr[$j]."; ";
    }
    $csv_output .= "\n";
    }
    $filename = $file."_".date("Y-m-d_H-i",time());
    header("Content-type: application/vnd.ms-excel");
    header("Content-disposition: csv" . date("Y-m-d") . ".csv");
    header( "Content-disposition: filename=".$filename.".csv");
    print $csv_output;
    exit;
    ?> 
    
    Code (markup):
    when I run this code, theres a pop up window asking if i want to open or save, when i click open or save i got an error.

    I got a pop up Window Internet Explorer

    Saying:
    Internet Explorer was not able to open this internet site. The requested site is either unavailable or cannot be found.Please try again.


    Thank you
     
    newphpcoder, Jan 26, 2012 IP
  2. yelbom

    yelbom Greenhorn

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #2
    Use phpMyAdmin and click on export and select the options to meet your requirements for csv file
     
    yelbom, Dec 9, 2012 IP
  3. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #3
    Why CSV? and dod you try BIGDUMP? search for it!
     
    EricBruggema, Dec 10, 2012 IP
  4. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #4
    Vooler, Dec 22, 2012 IP
  5. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #5
    A csv file isn't actually an excel file, it's a text file. (Most people use the default file associations, in which .csv is opened by Excel, but you can change that to open .csv files with a text editor.) Try changing your

    ("Content-type: application/vnd.ms-excel");

    into a header, and make it text/plain.
     
    Rukbat, Dec 22, 2012 IP
  6. rstoll

    rstoll Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #6
    If you using mysql then you can use the built in function of mysql to export the data
     
    rstoll, Dec 26, 2012 IP