SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    299
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help Me With Php Script For Exporting Records From Mysql Database

    Hello everyone,

    May some one help me with the php scripts that exports data(records) from mysql database to CVS format or text file with delimeter like comes. I have the script but it is not working properly. Please help me.

  2. #2
    SitePoint Wizard simplecode's Avatar
    Join Date
    Jun 2006
    Posts
    1,129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can you show us ur script

  3. #3
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,631
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Untested but should give you the general idea.
    PHP Code:
    $cvs "";
    $sql "SELECT * FROM table";
    $rs mysql_query ($sql);
    while ( 
    $row mysql_fetch_row ($rs) ) {
        
    $cvs .= join (','$row) . "\n";
    }
    $cvs trim ($cvs); 
    Change ',' to whatever delimiter you want to use.
    Ian Anderson
    www.siteguru.co.uk

  4. #4
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    299
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    below is my code

    <?php

    Output CSV on request
    if($CSV_x != "")
    {
    $this->HTML = "";
    // output CSV HTTP headers ...
    header("Cache-control: private");
    header("Content-type: application/force-download");

    if(strstr($_SERVER["HTTP_USER_AGENT"], "MSIE"))
    header("Content-Disposition: filename=data.csv" . "%20"); // For IE
    else
    header("Content-Disposition: attachment; filename=data.csv"); // For Other browsers

    // output CSV field names
    for($i = 0; $i < $FieldCount; $i++)
    $this->HTML .= mysql_field_name($result, $i) . $this->CSVSeparator;
    $this->HTML .= "\n\n";

    // output CSV data
    while($row = mysql_fetch_row($result))
    {
    for($i = 0; $i < $FieldCount; $i++)
    $this->HTML .= $row[$i] . $this->CSVSeparator;
    $this->HTML .= "\n\n";
    }
    $this->HTML = str_replace($this->CSVSeparator . "\n\n", "\n", $this->HTML);
    $this->HTML = substr($this->HTML, 0, strlen($this->HTML) - 1);

    // send output and quit script
    echo $this->HTML;
    exit;
    }
    $t = time(); // just a random number for any purpose ...
    ?>


Bookmarks

Posting Permissions

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