0

My Database is currently configured in a utf8 format and I am trying to export my database into a spreadsheet using PHP. I am using this spread sheet to import data into google analytics.

Though every time I try to import the code it gives me this error

"Invalid characters in the file. Only UTF-8 encoded characters are supported."

Any insight or a better way to export a mysql db to a csv utf8 format is what I am looking for.

This is the code I am using.

  function CreateCSV(){
  require_once("/var/www/html/config/DBConn.php");


  $select = "SELECT OrderID,Browser,Cookies,GaCampaign,GaContent,GaMedium,GaReferurl,GaSource,GaTerm,Gclid,IpAddress,GID,JavascriptEnabled,Mobile,OS,Pluginlist,ScreenDimension,Timestamp,UserId FROM OnlineSales ORDER BY OrderID DESC CHARACTER SET utf8";

  $export = mysql_query ( $select ) or die ( "Sql error : " . mysql_error( ) );

  $fields = mysql_num_fields ( $export );

  for ( $i = 0; $i < $fields; $i++ )
  {
         $header .= mysql_field_name( $export , $i ) . "\t";
  }
  $header= "ga:dimension1   ga:dimension12  ga:dimension17  ga:dimension10  ga:dimension9   ga:dimension7   ga:dimension11  ga:dimension6   ga:dimension8   ga:dimension5   ga:dimension3   ga:dimension4   ga:dimension16  ga:dimension14  ga:dimension13  ga:dimension18  ga:dimension15  ga:dimension19  ga:dimension2";
  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";
  }
  $data = str_replace( "\r" , "" , $data );

  if ( $data == "" )
  {
      $data = "\n(0) Records Found!\n";                        
  }


  $date = new DateTime();

  header ( 'Content-Type: application/vnd.ms-excel') ;
  header("Content-Disposition: attachment; filename=Export".$date->format('m-d-Y_H-i-s').".csv");
  header("Pragma: no-cache");
  header("Expires: 0");
  print "$header\n$data";

  }


  CreateCSV();
Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • 3
    Why not use [`fputcsv()`](http://php.net/manual/en/function.fputcsv.php)? – cpilko Feb 16 '15 at 21:51
  • I suggest you to try with this external library. It helped me a lot with csv export and import https://github.com/parsecsv/parsecsv-for-php – Alejandro Quiroz Feb 16 '15 at 21:57
  • [**Please, don't use `mysql_*` functions in new code**](http://stackoverflow.com/q/12859942). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://php.net/mysql_connect)? Learn about [*prepared statements*](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://php.net/mysqlinfo.api.choosing) will help you decide which. – cmbuckley Feb 17 '15 at 08:04
  • Is your [***connection encoding***](http://stackoverflow.com/a/279279/476) `utf8`? – deceze Feb 17 '15 at 08:05

0 Answers0