0

I try the code below:

$db = new mysqli('localhost', 'root', '', 'teste');
if(mysqli_connect_errno()){
echo mysqli_connect_error();
}

mysqli_query($db, "LOAD DATA INFILE 'dados.txt' INTO TABLE teste FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\n' (nome, idade, sexo)" );

echo "OK";

it returns ok but none row is insert in the table.

my file test is:

"Name Example 1",15,"F"
"Name Example 2",23,"M"
"Name Example 3",15,"M"
Italo Rodrigo
  • 1,555
  • 5
  • 17
  • 38

1 Answers1

0

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

Chandana Kumara
  • 2,485
  • 1
  • 22
  • 25
  • can you explain me a example of file? – Italo Rodrigo Oct 24 '16 at 16:01
  • 1
    ***Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).*** [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Oct 24 '16 at 16:11
  • pls check this way – Chandana Kumara Oct 24 '16 at 16:25
  • Pls do not do this kind of thing. Keep previous question here. This question add as new questions. Unless given answers not tally with question. – Chandana Kumara Oct 25 '16 at 02:18