Jump to content

Mysql import creates unwanted carriage return. How to remove?


dombrorj

Recommended Posts

Hello,

 

I have a simple text file that looks like...

53
23243
322
92443
3990

 

When I use Insert Into through a php script to import the data into my table, it enters the data into the database but adds an unwanted carriage return to the end of each value.

 

Here is the php script used to import the values...


<?php
    include '../config.php';
    mysql_query("TRUNCATE TABLE `master`");
   if($fh = fopen("numbers.txt","r")){
      while (!feof($fh)){
         $line = fgets($fh);
         if($line){
             echo "Importing value: $line <br />";
             mysql_query("INSERT INTO `master` (`numberid`) VALUES ('$line')");
         }

      }
      fclose($fh);
    }
?>

 

So now the value in 'number id' looks like...

53

23243

322

92443

3990

 

The extra carriage returns is causing problems. Is there an easy fix for this?

 

Thanks!

fgets() includes the newline character at the end of the input line. You can trim() the value to get rid of it.

 

         // trim() IT HERE
         $line = trim(fgets($fh));
         if($line){
             echo "Importing value: $line <br />";
             mysql_query("INSERT INTO `master` (`numberid`) VALUES ('$line')");

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.