views:

229

answers:

2

I need a php script to read a .txt file.

The content of the text file are like this:

data.txt

145|Joe Blogs|17/03/1954
986|Jim Smith|12/01/1976
234|Paul Jones|19/07/1923
098|James Smith|12/09/1998
234|Carl Jones|01/01/1925

These would then get stored into a database like this

*DataID |Name |DOB *

234    |Carl Jones|01/01/1925

I would be so grateful if someone could give me script to achieve this.

Update:

<?
$handle = @fopen("data.txt", "r");
$conn = mysql_connect("localhost","username","password"); 
mysql_select_db("mydatabase",$conn);
while (!feof($handle)) // Loop til end of file.
{
$buffer = fgets($handle, 4096);
 // Read a line.
list($a,$b,$c)=explode("|",$buffer);
//Separate string by the means of |
echo $a."-".$b."-".$c."<br>";
$sql = "INSERT INTO data_table (iddata, name, age) VALUES('".$a."','".$b."',".$c.")";   
mysql_query($sql,$conn) or die(mysql_error());
}
?>

get the following error error in your SQL syntax; ...for the right syntax to use near ')' at line 1

A: 

open txt file using fopen:
$handle = @fopen("xyz.txt", "r"); //read line one by one

$values='';

while (!feof($handle)) // Loop til end of file.

{

$buffer = fgets($handle, 4096); // Read a line.

list($a,$b,$c)=explode("|",$buffer);//Separate string by the means of |

//values.=($a,$b,$c);// save values and use insert query at last or

use mysql insert query here

}

THATS IT

nik
thanks, been working on following script but could not work out how transfer the content kept on get rows <br><?php$row = 1;if (($handle = fopen("data.txt", "r")) !== FALSE) { while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $num = count($data); echo "<p> $num fields in line $row: <br /></p>\n"; $row++; for ($c=0; $c < $num; $c++) { echo $data[$c] . "<br />\n"; } } fclose($handle);} for ($c=0; $c < $num; $c++) { echo $splitcontents[$c]; } ?>
Jack Brown
thanks your script works great
Jack Brown
@Jack Brown, if you need to execute a query anyways, why not use the `LOAD DATA INFILE` approach? You wouldn't have to worry about knowing how many values to set beforehand?
Anthony Forloney
Thanks but i need to get the file from another server using perhaps $filestring = file_get_contents("http://www.website.com/data.txt", "r"); Could i use that with LOAD DATA INFILE approach?
Jack Brown
data.txt needs to be pulled from another server, your scripts works wonderful but that's the only part im stuck on.
Jack Brown
I tried this<?$handle = @fopen("data.txt", "r");$conn = mysql_connect("localhost","username","password");mysql_select_db("mydatabase",$conn);while (!feof($handle)) // Loop til end of file.{$buffer = fgets($handle, 4096); // Read a line.list($a,$b,$c)=explode("|",$buffer);//Separate string by the means of |echo $a."-".$b."-".$c."<br>";$sql = "INSERT INTO data_table (iddata, name, age) VALUES('".$a."','".$b."',".$c.")";mysql_query($sql,$conn) or die(mysql_error());}?>get the following error error in your SQL syntax; ...for the right syntax to use near ')' at line 1
Jack Brown
@Jack Brown: When you want to post code, update the original question instead of posting it in the comment box.
Anthony Forloney
u can read the text file as$file="http://www.example.com/somefile.txt";fopen($file,"r");also make sure that allow_url_fopen is enabled in php.ini..You can check if file exist by using if(file_exists($file)) before opening it (fopen).
nik
Is there away to use $string = file_get_contents("http://www.website.com/data.txt", "r"); with your script? Im sorry but my hosting company wont let me save the file. If i change $handle = @fopen("xyz.txt", "r") to $handle = file_get_contents("http://www.website.com/data.txt", "r"); I get an error with eof($handle). Any ideas?
Jack Brown
+3  A: 

What you may be looking for is MySQL's built-in function LOAD DATA INFILE to load a text file containing values for a database into a database.

The LOAD DATA INFILE statement reads rows from a text file into a table at a very high speed. The file name must be given as a literal string.

Example:

LOAD DATA INFILE 'data.txt' INTO TABLE my_table;

You could also specify the delimiters inside of your text-file, like so:

LOAD DATA INFILE 'data.txt' INTO TABLE my_table FIELDS TERMINATED BY '|';

Update:

Here is a full-working example, I uploaded a test data file here and here is my PHP code.

$string = file_get_contents("http://www.angelfire.com/ri2/DMX/data.txt", "r");
$myFile = "C:/path/to/myFile.txt";
$fh = fopen($myFile, 'w') or die("Could not open: " . mysql_error());
fwrite($fh, $string);
fclose($fh);

$sql = mysql_connect("localhost", "root", "password");
if (!$sql) {
    die("Could not connect: " . mysql_error());
}
mysql_select_db("my_database");
$result = mysql_query("LOAD DATA INFILE '$myFile'" .
                      " INTO TABLE test FIELDS TERMINATED BY '|'");
if (!$result) {
    die("Could not load. " . mysql_error());
}

Here what the table looked before running my PHP code:

mysql> select * from test;
+--------+-----------+------------+
| DataID | Name      | DOB        |
+--------+-----------+------------+
|    145 | Joe Blogs | 17/03/1954 |
+--------+-----------+------------+
1 row in set (0.00 sec)

And here is the result after:

mysql> select * from test;
+--------+-------------+------------+
| DataID | Name        | DOB        |
+--------+-------------+------------+
|    145 | Joe Blogs   | 17/03/1954 |
|    234 | Carl Jones  | 01/01/1925 |
|     98 | James Smith | 12/09/1998 |
|    234 | Paul Jones  | 19/07/1923 |
|    986 | Jim Smith   | 12/01/1976 |
+--------+-------------+------------+
5 rows in set (0.00 sec)
Anthony Forloney
Thank you soo much. I spent hours trying to get it work. As you've guessed im an amateur to php. Anthony my next task is download the file every 24 hours. Should i use date() or time() function to compare time? and does this involve the server set-up in a different way ie cron? However not sure how cron works. Perhaps there is a work around ie when someone enters the website it checks an old time with the new time and then triggers the above code in a function. Do database tables have a time stamp when they were updated or inserted?
Jack Brown
Just a thought ... if the data.txt file needs to be downloaded every 24 hours then then database needs to be updated with the content.
Jack Brown
Found current timestamp in mysql. Type:Timestamp Default:Current_timestamp Attributes:on update Current_timestamp. It adds date and time to each row. Does slow down the database? So now im trying to work out how to make it all work. 1, download the file and load contents to database (which you wrote a great script). 2, check every 24 hours and update the contents.
Jack Brown
Cron is something I am not familiar with but it seems to have exactly what you want. In regards to current timestamp, in my experience it does not seem to produce a big performance hit to use it, but worry about performance when it's *time* to worry about performance.
Anthony Forloney
I cant use 'LOAD DATA INFILE..' and save file to the server due to hosting company. After all that :( Is there an alternative method? Im having problems amending your script so that it can grab the data using file_get_contents() next transferring into an array using explode and inserting/updating using accessible sql statements.
Jack Brown
@Jack Brown: It's hard for me to envision exactly what you are trying to achieve and what your road blocks you reach. Using `file_get_contents` will return a `string` which will make it hard for you to determine when the `\n`'s are. I cannot think of an alternative method to what you might need done. I wish I could help you out more.
Anthony Forloney