-1

I want to import a csv file content into a database table, I'm using this code that it works perfectly when pasting it in phpmyadmin console:

LOAD DATA LOCAL INFILE '/Applications/MAMP/htdocs/testApp/trips.csv' INTO TABLE trips FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' IGNORE 1 ROWS (type,startDate,endDate,steps,coordinates,distance)

However, when using it in a php file, I have an error:

<?php

$host       = "localhost"; //Your database host server
$db         = "dbTest"; //Your database name
$user       = "root"; //Your database user
$pass       = "root"; //Your password
$connection = mysqli_connect($host, $user, $pass);

//Check to see if we can connect to the server
if (!$connection) {
    die("Database server connection failed.");
    die(mysqli_error($db));
} else {
    //Attempt to select the database
    $dbconnect = mysqli_select_db($connection, $db);
    //Check to see if we could select the database
    if (!$dbconnect) {
        die("Unable to connect to the specified database!");
    } else {
        $sql = "LOAD DATA LOCAL INFILE '/Applications/MAMP/htdocs/testApp/trips.csv' 
                INTO TABLE trips
                FIELDS TERMINATED BY ',' 
                LINES TERMINATED BY '\\n' 
               IGNORE 1 ROWS (type,startDate,endDate,steps,coordinates,distance)"
                ;

        $result = mysql_query($sql, $connection);

        if (mysql_affected_rows() == 1) {
            $message = "The trip was successfully inserted!";
        } else {
            $message = "The trip insert failed";
            $message .= mysql_error();
        }

        echo $message;
    }
}
?>

=> The trip insert failed

I'm pretty sure that the problems come from a \ or any other character that I can't target. Thank you for helping .

androniennn
  • 3,117
  • 11
  • 50
  • 107

3 Answers3

2

You've mixed up MySQL and MySQLi plugins — you're trying to connect with MySQLi then perform a query with MySQL. You can't do that. Pick one API and use it consistently.

So:

mysql_query($sql)     ---> $connection->query($sql)
mysql_affected_rows() ---> $result->affected_rows()
mysql_error()         ---> $connection->error

Ultimately, this has nothing to do with LOAD DATA INFILE and you should have tried it with a basic SELECT! And I recommend reading the documentation for the functions that you use.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
1

EDIT : You are mixing two plugins MYSQL and MYSQLi in same script, below i dumped remaining part of your code using mysqli plugin, because your upper part uses mysqli...

N.B:You can't do that. Pick just any single API.

    $result = mysqli_query($connection, $sql);

    if (mysqli_affected_rows($connection) >= 1) {
        $message = "The trip was successfully inserted!";
    } else {
        $message = "The trip insert failed";
        $message .= mysqli_error($connection);
    }
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
  • A good answer explains what is wrong rather than just dumping replacement code. – Lightness Races in Orbit Dec 26 '14 at 12:12
  • @LightnessRacesinOrbit, i have already commented that answer 45+ mins ago you can see it in comment section, thats why here i just dumped replacement answer....ok never mind for your down vote...mam,best of luck – A l w a y s S u n n y Dec 26 '14 at 12:15
  • Comments are insufficient. Comments can be removed at any time. Comments supplement answers, not the other way around. The useful information from comments should be integrated into answers. – Lightness Races in Orbit Dec 26 '14 at 12:16
  • @LightnessRacesinOrbit, ok i'll try to follow it mam, thanks for advice/suggestion.... – A l w a y s S u n n y Dec 26 '14 at 12:17
  • You solved my question dear Always Sunny, and you made my day more sunny! Your replacement code gains me more time, and unfortunately i'm not a php expert. Thank you . – androniennn Dec 26 '14 at 13:53
  • Congratulations on stealing my answer (basically word for word!) and winning an accept with it. Good job. – Lightness Races in Orbit Dec 27 '14 at 03:16
  • words are insufficient. words can be removed/change at any time as you said to me previously to my comment @LightnessRacesinOrbit. thanks for down voting my answer. Be broad minded, you already 137k in your bag..... – A l w a y s S u n n y Dec 27 '14 at 03:20
  • Not the point. Plagiarism is _not_ cool, and swapping six of my words out for a lightly paraphrased version now that I've called you out on it is not fooling anybody. As for "words can be removed/change[d] at any time", no I didn't say that, I said _comments_ can be. Don't misrepresent me now! – Lightness Races in Orbit Dec 27 '14 at 03:25
  • i am just trying to mean that, you previously said,"Comments can be removed at any time", then words can also be changed/removed at any time. i am not misrepresenting you, sorry for my bad English. – A l w a y s S u n n y Dec 27 '14 at 03:33
-1

Try to move you trip.csv file into datadir - You can find it with this command : SELECT @ @datadir

toto21
  • 612
  • 5
  • 9