Connect with Expertise | Find Experts, Get Answers, Share Insights

how can i write my data from text file into database

 
Join Date: Jan 2010
Posts: 15
#1: Jan 28 '10
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
Expand|Select|Wrap|Line Numbers
  1.  <html>
  2. <?
  3.  
  4. $con=mysql_connect("localhost","root","root");
  5. if (!$con)
  6.   {
  7.   die('Could not connect: ' . mysql_error());
  8.   }
  9.  
  10. mysql_select_db('test',$con);
  11. //(1) Read the text file into a variable
  12. $file = "testfile.txt";
  13. $fp = fopen($file, "r");
  14. $data = fread($fp, filesize($file));
  15. fclose($fp);
  16.  
  17.  
  18. //(2) Then we can get rid of the tabs in there:
  19.  
  20. $output = str_replace("\t|\t", "|", $data);
  21.  
  22.  
  23. //(3) Then we explode it at every line break
  24.  
  25. $output = explode("\n", $output);
  26.  
  27.  
  28. //(4) Then we loop through all the array elements and explode them again and insert them into mysql
  29.  
  30. foreach($output as $var)
  31. $tmp = explode("|", $var);
  32. $FirstName = $tmp[0];
  33. $LastName = $tmp[1];
  34. $Age = $tmp[2];
  35. $sql = "INSERT INTO student SET FirstName='$FirstName', LastName='$LastName',Age='$Age'";
  36. mysql_query($sql);
  37.  
  38.  
  39.  
  40. echo "Done!";
  41. mysql_close($con);
  42. ?>     </html>
  43.  
.

Atli's Avatar
E
M
C
 
Join Date: Nov 2006
Location: Iceland
Posts: 4,681
#2: Jan 28 '10

re: how can i write my data from text file into database


Hey.

First of all, your foreach loop is - undoubtedly - not working as you want it to. Anything that should belong to a loop should be put inside brackets following the loop.

If the foreach is not followed by brackets ({}), it will assume only the very first line following it belongs to the loop. Therefore you should ALWAYS use brackets. Even if there is only one line. (For the sake of consistency)
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // Some random values to loop through.
  3. $values = array("first", "second", "third");
  4.  
  5. // This only prints one value: "third"
  6. foreach($values as $_value)
  7.     $value = $_value; // This is inside the loop
  8.     echo $value; // This is OUTSIDE the loop.
  9.  
  10. // This prints all three values.
  11. foreach($values as $_value) {
  12.     $value = $_value;
  13.     echo $value;
  14. }
  15. ?>
Also, you should verify that the data is valid before using it in the query. Make sure the values you are using aren't empty, and that they contain the data you expect them to contain.

And as always, when dealing with MySQL queries: run ALL values through mysql_real_escape_string before putting it into the query. Otherwise your database is open for SQL Injection attacks. (If you think the data you are using is secure... you are wrong! :P)
dgreenhouse's Avatar
E
C
 
Join Date: May 2008
Location: San Francisco
Posts: 154
#3: Jan 28 '10

re: how can i write my data from text file into database


I see said the blind man... :-) I didn't notice the brackets... :-)

Is that really how the data is delimited?
i.e. with \t|\t

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // This works...
  3. $b = "\t|\t";
  4. $input = "John\t|\tSmith\nMary\t|\tJohnson";
  5. $output = str_replace($b,'|',$input);
  6. $output = explode("\n",$output);
  7. foreach($output as $var) {
  8.   $tmp = explode("|",$var);
  9.   echo $tmp[0] . ' ' . $tmp[1] . '<br>';
  10. }
  11.  
  12. ?>
  13.  
Also, if the file isn't very large, you can use the 'file()' command to create an array of lines. Then you can explode each line (as you're doing) on your replaced delimiter ('|').
 
Join Date: Jan 2010
Posts: 15
#4: Feb 1 '10

re: how can i write my data from text file into database


thanx for d help..........
Reply