How can I force a Integer-to-String export to Excel using php/mysql? -


i need export data mysql table excel. i'm using code below, working fine.

the problem is: there 2 fields integers more 30 digits. when export .csv file, excel transforms them scientific notation.

example: field "95145612345641859415634194164163" transforms "9.51456e+31"

how can force read string?

<?php   exportmysqltocsv('export_csv.csv');    function exportmysqltocsv($filename = 'export_csv.csv') {     $conn = dbconnection();      if ($conn->connect_error) {         die("connection failed: " . $conn->connect_error);     }     $sql_query = "select * cadastros";       $result = $conn->query($sql_query);      $f = fopen('php://temp', 'wt');     $first = true;     while ($row = $result->fetch_assoc()) {         if ($first) {             fputcsv($f, array_keys($row));             $first = false;         }         fputcsv($f, $row);     } // end while      $conn->close();      $size = ftell($f);     rewind($f);      header("cache-control: must-revalidate, post-check=0, pre-check=0");     header("content-length: $size");     header("content-type: text/x-csv");     header("content-type: text/csv");     header("content-type: application/csv");     header("content-disposition: attachment; filename=$filename");     fpassthru($f);     exit;  }  // db connection function function dbconnection(){     $servername = "localhost";     $username = "root";     $password = "";     $dbname = "creche_escolas";     // create connection     $conn = new mysqli($servername, $username, $password, $dbname);     return $conn; } 

you should make sure number string when insert value, try:

fputcsv($f, ['12343231743986248756324354595', '123456', 12123437829564329758623456789]); 

the first value stay same while last converted 1.21234378296e+28

so need replace line fputcsv($f, $row); function convert strings such number_format:

fputcsv($f, array_map(function($v){return number_format($v, 0, '', '');}, $row)); 

Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -