ainoy31 Posted June 12, 2008 Share Posted June 12, 2008 Hello. I am trying to read a text file data and then insert these data into a mysql database. Here is the sample text file data: CRW|2007|CADILLAC|ESCALADE|V8|6.2|GAS|FI|N|8|L92|Grille Insert|37012|Billet Aluminum Grille; CNC Machined; Marque-Mesh;No Cutting; Polished Here is my code that I am trying: $data = readfile('misc files/part.txt'); $lines = explode("|", $data); echo $lines[0]; I should get CRW on my echo but I am getting the whole string. Either I am too tired or need to sleep and overlooking this. Much appreciation. AM Quote Link to comment https://forums.phpfreaks.com/topic/109845-solved-read-a-text-file-and-insert-into-the-db/ Share on other sites More sharing options...
ainoy31 Posted June 12, 2008 Author Share Posted June 12, 2008 If I use the file_get_contents() command to read the file, it will return the data as a string. Then, I can explode the data and get the different peices of data I need to insert into the DB. Quote Link to comment https://forums.phpfreaks.com/topic/109845-solved-read-a-text-file-and-insert-into-the-db/#findComment-563677 Share on other sites More sharing options...
sKunKbad Posted June 12, 2008 Share Posted June 12, 2008 try using print_r($lines), and see what the array looks like. Quote Link to comment https://forums.phpfreaks.com/topic/109845-solved-read-a-text-file-and-insert-into-the-db/#findComment-563678 Share on other sites More sharing options...
bluejay002 Posted June 12, 2008 Share Posted June 12, 2008 you may try this: <?php $filename = "misc files/part.txt"; $handle = fopen($filename, "r"); $data = fread($handle, filesize($filename)); fclose($handle); $lines = explode("|", $data); foreach($lines as $line) { echo $line . "<br />"; } ?> or <?php $filename = "misc files/part.txt"; $data = file_get_contents($filename); $lines = explode("|", $data); foreach($lines as $line) { echo $line . "<br />"; } ?> either would work... MODIFIED: oops... i added this a bit too late Quote Link to comment https://forums.phpfreaks.com/topic/109845-solved-read-a-text-file-and-insert-into-the-db/#findComment-563683 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.