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

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b38489-2c107.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b38489-2c107.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?