Italo Rodrigo Italo Rodrigo - 1 month ago 16
MySQL Question

How do Upload and execute a file in a server PHP?

I have a firebird local database with 25k rows. and I need to send the registers to a online mysql server. I can to do it but is very slow.

so I have the idea: to create a txt file with the sql commands:

insert into table (a, b) values (a, b);
insert into table (a, b) values (a, b);
...
insert into table (a, b) values (a, b);


I want to upload this file in my php server and execute it to insert the values in my table. this is possible?

Answer

You can do this way.

LOAD DATA INFILE 'datafile.txt' INTO TABLE [Table_Name];

Create data file as below:

1 Test1 Name1

2 Test2 Name2

3 Test2 Name2

Then create table according to correct data types and names.

CREATE TABLE [Table_Name](id INT, col1 VARCHAR(255), col2 VARCHAR(255));

$sql = mysql_connect("localhost", "root", "password");
if (!$sql) {
   die("Could not connect: " . mysql_error());
}
mysql_select_db("database_name");
$result = mysql_query("LOAD DATA INFILE 'datafile.txt'" .
                  " INTO TABLE [Table_Name] FIELDS TERMINATED BY '\t'");
if (!$result) {
   die("Could not load. " . mysql_error());
}

datafile.txt path should give correctly.

Refer:http://php.net/manual/en/mysqli.set-local-infile-handler.php