hello everyone
i m inserting data from text file into database. .......but the data into the text file is not inserted into database only blank record display in the database..........
my text file is like this:
james flower 22
may be i m not giving space delimiter correct... 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 = "testFile.txt";
$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", $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='$FirstName', LastName='$LastName',Age='$Age'";
mysql_query($sql);
echo "Done!";
}
mysql_close($con);
?> </html>