简体   繁体   中英

LOAD DATA INFILE error in php

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 .

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.

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...

NB :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);
    }

尝试你trip.csv文件移动到datadir -您可以使用此命令找到它: SELECT @ @datadir

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM