0

hello dr..........
i m trying to store a text file into database below format of text file
i hv text file like this:
0100102001110271 0409141151I1476201
0100102001110125 0409141351O1476301
0100101300000030 0409141630O1476401
0100102001110271 0409141631O1476501
0100102001110126 0409141632O1476601

how can store it into my database table att0310
my table fileds:
cardno date time IO(in-out) and serial no
0100102001110271 this is card no
040914 for date
1151 for time
I for IO
1476201 for serial no
plz help me out

till date data is inserted correctly into database but next data not stored into database. because of there is no space b/w those digits .... hw can i store that into database.... i tried so many times......... i used fopen function to read file character by character but i only display dat on browser not into database .. plz help me out.........

<html>
<?
$con=mysql_connect("localhost","root","root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('test',$con);
//(1) Read the text file into a variable
$file=fopen("ATND.txt","r") or exit("Unable to open file!");

//(2) Then we can get rid of the tabs in there:
$output = str_replace("\t"," ", $file);
//(3) Then we explode it at every line break
$output = explode("\n", $output);
//(4) Then we loop through all the array elements and explode them again and insert them into mysql
foreach($output as $var)
{
$tmp = explode(" ", $var);
//$ecode = $tmp[0];


$ecardno = $tmp[0];
$atdate = $tmp[1];
$attime = $tmp[2];
 $IO = $tmp[3];
$scode = $tmp[4];
$sql = "INSERT INTO att SET  ecardno='$ecardno',atdate='$atdate',attime='$attime',IO='$IO',serialno='$serialno'";
mysql_query($sql);
}
echo "Done!";
mysql_close($con);
?>
</html>
2
Contributors
3
Replies
5
Views
7 Years
Discussion Span
Last Post by urtrivedi
0

If the string length is fixed then you can use "substr" function instead of explode.
1) Read one line at time into a variable
$dataline=readnextline($filepointer);
2) store output of substr function in proper column variable
$col1=substr($dataline,1,3);
$col2=substr($dataline,5,1);
3) then build a insert /update query using column variables
insert into table1 (col1,col2) values('$col1','$col2');

0

can u plz elaborate it ...... i could nt understand ur code

0

I have modified line 24 to 27 of your code. I have used substr function.

<html>
<?
$con=mysql_connect("localhost","root","root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('test',$con);
//(1) Read the text file into a variable
$file=fopen("ATND.txt","r") or exit("Unable to open file!");
//(2) Then we can get rid of the tabs in there:
$output = str_replace("\t"," ", $file);
//(3) Then we explode it at every line break
$output = explode("\n", $output);
//(4) Then we loop through all the array elements and explode them again and insert them into mysql
foreach($output as $var)
{
$tmp = explode(" ", $var);
//$ecode = $tmp[0];
$ecardno = $tmp[0];
$atdate = substr($tmp[1],0,6);
$attime = substr($tmp[1],6,4);
$IO = substr($tmp[1],10,1);
$scode = substr($tmp[1],11,7);
$sql = "INSERT INTO att SET ecardno='$ecardno',atdate='$atdate',attime='$attime',IO='$IO',serialno='$serialno'";
mysql_query($sql);
}
echo "Done!";
mysql_close($con);
?>
</html>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.