Export a mysql table into CSV using PHP code

前端 未结 4 1213
猫巷女王i
猫巷女王i 2021-01-20 22:56

I have a my sql table called pvdata, I would like to export it to csv file.

But I\'m obtaining the following results instead of the normal looking table:



        
相关标签:
4条回答
  • 2021-01-20 23:16

    Before for loop initialize $header to empty string.

    $header = '';//initialize header
    for ( $i = 0; $i < $fields; $i++ )
    {
        $header .= mysql_field_name( $export , $i ) . "\t";
    
        echo $header;//remove this line
    }
    

    EDIT

    Also initialize $data outside while loop.

    $data = '';
    while( $row = mysql_fetch_row( $export ) )
    {
        $line = '';
        foreach( $row as $value )
        {                                            
            if ( ( !isset( $value ) ) || ( $value == "" ) )
            {
                $value = "\t";
            }
            else
            {
                $value = str_replace( '"' , '""' , $value );
                $value = '"' . $value . '"' . "\t";
            }
            $line .= $value;
        }
        $data .= trim( $line ) . "\n";
    }
    
    0 讨论(0)
  • 2021-01-20 23:17

    Try this code -

    <?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 = time().'csv'; //For unique file name
        header('Content-type: application/csv');
        header('Content-Disposition: attachment; filename='.$filename);
    
        echo $output;
        exit;
    
        ?>
    

    Courtesy - Export MySQL table to CSV using PHP

    0 讨论(0)
  • 2021-01-20 23:19

    I don't know if it is a typo, but within your script are spaces before the

       <?php
    

    this will result in an html document being delivered and your header-calls will fail. So remove the spaces and any other output before "header" calls.

    You are getting error messages up there, for a quick solution try:

    error_reporting(0);
    

    Line 28: $header is not known, this makes

    $header .=
    

    appending to a unknown variable.

    0 讨论(0)
  • 2021-01-20 23:32

    You can use fputcsv function for clear code.

    0 讨论(0)
提交回复
热议问题