cluce Posted July 3, 2007 Share Posted July 3, 2007 I wrote a loop that reads a csv file which contains passwords for users in a table. and when I run my script, it reads the file fine and updates the passwords next to the appropriate user but it adds two little squares behind the passord. for example, password[][]. it looks something like that. does anybody know why this is? my code does work but I post it here just to give a bigger picture of what im doing. <?php $counter = 1; //initialize counter include'db.php'; $filename = "pass.csv"; $fp = fopen($filename, "r") or die("Couldn't open $filename"); while (!feof($fp)) { $line = fgets($fp, 1024); $sql3 = "UPDATE employees SET password = '$line' WHERE EmployeeID = '$counter' LIMIT 1"; mysqli_query($mysqli, $sql3); echo "$counter<br>$line<br>"; //echo output of each count*2 $counter++; //adds 1 to counter } //} ?> Quote Link to comment Share on other sites More sharing options...
per1os Posted July 3, 2007 Share Posted July 3, 2007 <?php $counter = 1; //initialize counter include'db.php'; $filename = "pass.csv"; $file = file($filename); foreach ($file as $line) { $sql3 = "UPDATE employees SET password = '$line' WHERE EmployeeID = '$counter' LIMIT 1"; mysqli_query($mysqli, $sql3); echo "$counter<br>$line<br>"; //echo output of each count*2 $counter++; //adds 1 to counter } ?> New line character, use file to read in the file (it puts it into an array split at the new line). Quote Link to comment Share on other sites More sharing options...
cluce Posted July 3, 2007 Author Share Posted July 3, 2007 thanks for clearing that up. I tried the file() to read it but it does the same. Quote Link to comment Share on other sites More sharing options...
thedarkwinter Posted July 3, 2007 Share Posted July 3, 2007 Hi Or just do what you have except trim the contents of the line you read... <?php //... $line = trim(fgets($fp, 1024)); cheers, tdw Quote Link to comment Share on other sites More sharing options...
cluce Posted July 3, 2007 Author Share Posted July 3, 2007 thanks..the trim function worked. FYI.....about the other code I was able to delete one square by doing this... file($filename,FILE_IGNORE_NEW_LINES); Quote Link to comment 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.