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
Expand|Select|Wrap|Line Numbers
- <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|\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>