php parsing/read text file
Moderator: General Moderators
php parsing/read text file
Hi to all!! I am still new to php.. I need help in reading my text file and inserting it to mysql database.
My text file contains 5 fields and has several rows..
The format of my text file:
1 romeo 8877 2010-07-07 abc1
2 nick 7686 2010-07-07 abc2
3 mark 5456 2010-07-07 abc3
4 karm 3432 2010-07-07 abc4
The first field is an INTEGER, the second third fifth fields are VARCHAR and the fourth field is a DATE.
I am having trouble with my php code..
My code is this:
<?php
mysql_connect("localhost","root","");
mysql_select_db("personss");
$textFileName = $_POST['textFileName'];
$myFile = "$textFileName";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
$person = explode("\n", $theData);
$count = 0;
foreach($person as $i => $line) {
$nameParts = explode(" ", $person[$count]);
$q = "insert into logged (id, name, num, datess, letters) values ('" . implode("','",$nameParts) . "')";
$rw = mysql_query($q) or die("Problem with the query: $q<br>" . mysql_error());
}
?>
This code can insert to the database but it only inserts the first row and it loops the insert according to how many rows there are in the text file..For example in my text file there are are 4 rows, it inserts the first row: 1 romeo 8877 2010-07-07 abc1 4times..If there are 6 rows, 6 times the first row will also be inserted..
What i need Sir's is that i need to be able to insert the rows line by line.. if there are 4 rows, i need to insert them all in the database.. Please help me to fix the code.
My text file contains 5 fields and has several rows..
The format of my text file:
1 romeo 8877 2010-07-07 abc1
2 nick 7686 2010-07-07 abc2
3 mark 5456 2010-07-07 abc3
4 karm 3432 2010-07-07 abc4
The first field is an INTEGER, the second third fifth fields are VARCHAR and the fourth field is a DATE.
I am having trouble with my php code..
My code is this:
<?php
mysql_connect("localhost","root","");
mysql_select_db("personss");
$textFileName = $_POST['textFileName'];
$myFile = "$textFileName";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
$person = explode("\n", $theData);
$count = 0;
foreach($person as $i => $line) {
$nameParts = explode(" ", $person[$count]);
$q = "insert into logged (id, name, num, datess, letters) values ('" . implode("','",$nameParts) . "')";
$rw = mysql_query($q) or die("Problem with the query: $q<br>" . mysql_error());
}
?>
This code can insert to the database but it only inserts the first row and it loops the insert according to how many rows there are in the text file..For example in my text file there are are 4 rows, it inserts the first row: 1 romeo 8877 2010-07-07 abc1 4times..If there are 6 rows, 6 times the first row will also be inserted..
What i need Sir's is that i need to be able to insert the rows line by line.. if there are 4 rows, i need to insert them all in the database.. Please help me to fix the code.
-
shawngoldw
- Forum Contributor
- Posts: 212
- Joined: Mon Apr 05, 2010 3:38 pm
Re: php parsing/read text file
what about $count++ after you explode person[count]
shawn
shawn
Re: php parsing/read text file
I tried but didnt work Sir
-
shawngoldw
- Forum Contributor
- Posts: 212
- Joined: Mon Apr 05, 2010 3:38 pm
Re: php parsing/read text file
On closer look, you don't actually need $person[$count] or $count at all. Replace $person[$count] with $line.
Re: php parsing/read text file
uhm..it worked Sir..tnx very much!!.
One last thing.. after each insert in the fifth field Sir.. a space is included..and in my mysql it is displayed as a special character just beside after the last character of the fifth field
One last thing.. after each insert in the fifth field Sir.. a space is included..and in my mysql it is displayed as a special character just beside after the last character of the fifth field
- Attachments
-
- this the special char
- Untitled.jpg (1.44 KiB) Viewed 1227 times
-
shawngoldw
- Forum Contributor
- Posts: 212
- Joined: Mon Apr 05, 2010 3:38 pm
Re: php parsing/read text file
Try changing this:
to:
Also please, you don't have to call me sir
Shawn
Code: Select all
$person = explode("\n", $theData);Code: Select all
$person = explode("\r\n", $theData);Shawn
Re: php parsing/read text file
WOW!! WORKED LIKE A CHARM!! TY VERY MUCH SIR!
SIR is my sign of respect to you.
SIR is my sign of respect to you.
-
shawngoldw
- Forum Contributor
- Posts: 212
- Joined: Mon Apr 05, 2010 3:38 pm
Re: php parsing/read text file
No problem.