hi..
i m trying to write data from txt file onto database but when i am executing my php code for it the empty record will be inserted in the database not the data written on txt file will be entered.plz help me out
[code=php]
<html>
<?
$con=mysql_conn ect("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 = "testfile.t xt";
$fp = fopen($file, "r");
$data = fread($fp, filesize($file) );
fclose($fp);
//(2) Then we can get rid of the tabs in there:
$output = str_replace("\t |\t", "|", $data);
//(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);
$FirstName = $tmp[0];
$LastName = $tmp[1];
$Age = $tmp[2];
$sql = "INSERT INTO student SET FirstName='$Fir stName', LastName='$Last Name',Age='$Age '";
mysql_query($sq l);
echo "Done!";
mysql_close($co n);
?> </html>
[/code].
i m trying to write data from txt file onto database but when i am executing my php code for it the empty record will be inserted in the database not the data written on txt file will be entered.plz help me out
[code=php]
<html>
<?
$con=mysql_conn ect("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 = "testfile.t xt";
$fp = fopen($file, "r");
$data = fread($fp, filesize($file) );
fclose($fp);
//(2) Then we can get rid of the tabs in there:
$output = str_replace("\t |\t", "|", $data);
//(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);
$FirstName = $tmp[0];
$LastName = $tmp[1];
$Age = $tmp[2];
$sql = "INSERT INTO student SET FirstName='$Fir stName', LastName='$Last Name',Age='$Age '";
mysql_query($sq l);
echo "Done!";
mysql_close($co n);
?> </html>
[/code].
Comment