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)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…