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 4 of 4
  1. #1
    Regular Coder
    Join Date
    Aug 2010
    Posts
    713
    Thanks
    165
    Thanked 0 Times in 0 Posts

    Back Up database as csv file

    Hi


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

    I found this code:

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

  • #2
    Regular Coder
    Join Date
    Jan 2012
    Posts
    271
    Thanks
    2
    Thanked 65 Times in 65 Posts
    .. edit - removed --

    sorry.. didn't read the question properly..

    I can't see anything in there that is creating a file, it's just echoing to screen... but the header is telling the browser there's a file but there is actually no file...
    Last edited by jmj001; 01-27-2012 at 03:43 AM.

  • #3
    Regular Coder
    Join Date
    Jan 2012
    Posts
    134
    Thanks
    0
    Thanked 32 Times in 32 Posts
    Quote Originally Posted by jmj001 View Post
    it's just echoing to screen... but the header is telling the browser there's a file but there is actually no file...
    Setting a proper header and then echoing content tells the browser to treat that content as a certain file type.

    newphpcoder, comment out the three lines at the end starting with header and then go to the web page. If there are PHP errors, then there's a problem with your export script. If you actually see a proper CSV file in your browser, then there's an issue with the headers.

  • #4
    Regular Coder
    Join Date
    Jan 2012
    Posts
    271
    Thanks
    2
    Thanked 65 Times in 65 Posts
    .. ..


  •  

    Posting Permissions

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