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!

Link to comment
Share on other sites

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')");

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.