View Full Version : How to read HTML or TXT file and output the data?
koolkicks311
04-21-2007, 04:40 AM
Does anyone know if this is possible?
Script reads a HTML file or a TXT file and output the data to MySQL database.
neel_basu
04-21-2007, 05:50 AM
insert into table with
file_get_contents('the_file.htm');
More specifically, to read the text from the file into a string
$string = file_get_contents('file.txt');
//or
$string = file_get_contents('http://www.whatever.com/file.htm');
and to put it into a database, would be something along the lines of
mysql_query("INSERT INTO table (field) VALUES ('". $string "')");
Replacing table and field
That's how I would do it anyway, although you don't necessarily have to escape the string like I did
Umang
04-21-2007, 01:35 PM
Just to add to that, you'd like to mysql_real_escape_string that. Like:
mysql_query("INSERT INTO table (field) VALUES ('". mysql_real_escape_string($string) . "')");
And you missed a "." too.
Ah yeah so I did thanks, and yeah, mysql_real_escape_string is good to have in there as well
syosoft
04-24-2007, 04:25 AM
Have you looked into the LOAD DATA INFILE mysql offers...might be a far more elegant solution and is MUCH faster for mass inserts.
It's worth a look. For a project i'm involved in, i had to insert up to 10k 2 column records at a given time. After noticing it was horribly slow to loop inserts, i found this function and once i had php create a file mysql could use with LOAD DATA INFILE the overall time went from abotu 35 seconds to ~1 sec.
http://dev.mysql.com/doc/refman/5.0/en/load-data.html
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.