dombrorj Posted January 31, 2011 Share Posted January 31, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/226286-mysql-import-creates-unwanted-carriage-return-how-to-remove/ Share on other sites More sharing options...
DavidAM Posted January 31, 2011 Share Posted January 31, 2011 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')"); Quote Link to comment https://forums.phpfreaks.com/topic/226286-mysql-import-creates-unwanted-carriage-return-how-to-remove/#findComment-1168101 Share on other sites More sharing options...
dombrorj Posted January 31, 2011 Author Share Posted January 31, 2011 Thanks David!! Quote Link to comment https://forums.phpfreaks.com/topic/226286-mysql-import-creates-unwanted-carriage-return-how-to-remove/#findComment-1168105 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.