Italo Rodrigo Italo Rodrigo - 8 months ago 51
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?


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());
$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.