Write an API through php to write the data from the txt file to the database

now the project needs to write an API to write the data from the txt file to the database. The txt file format is as follows:
1 Test data-English
2 Test data 2 chinese
3 Test data 3 American

now insert
$conn = mysql_connect after modification with the following code. Mysql_error ());
mysql_select_db ("test", $conn) or die ("Invalid query:". Mysql_error ());

$content = file_get_contents ("serial_number.txt");
$contents= explode (",", $content);

foreach ($contents as $k = > $v)
{
$id = $k;
$serial_number = $v;
mysql_query ("insert into serial_number ( id , serial_number )

)

  VALUES("$id","$serial_number")");

}
there is no problem when inserting a small amount of data, but if 300000 of the data does not seem to be very good, I would like to ask for leave. Do you have a better way

Php
Mar.02,2021

you can import data using MySQL's LOAD DATA instruction, which is much faster than INSERT

http://www.runoob.com/mysql/m.


you just need to change the way the file is read. Do not use the file_get_contents () method, which will read the whole file into memory at once. Read line by line instead:

  

LOAD DATA / mysqlimport
or PHP changes the reading mode yield

Menu