Suffii
Suffii

Reputation: 5784

Having Issue On MySQLi LOAD DATA INFILE to Load .CSV File

I am trying to load a 40 mg .CSV file into MYSQL database using MySQLi and PHP but I am getting only The user update failed: message (Witdout Error Message!) after loading the page

<?PHP   
   define ( 'DB_HOST', 'localhost' );
   define ( 'DB_USER', 'root' );
   define ( 'DB_PASS', '' );
   define ( 'DB_NAME', 'map' );


$con = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);

if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
};
$sql = "LOAD DATA INFILE 'C:/wamp/www/UP/Modified_Single.csv'
       INTO TABLE  `single-tbl` 
       FIELDS TERMINATED BY ','
       OPTIONALLY ENCLOSED BY '\"' 
       LINES TERMINATED BY '\n' 
       IGNORE 1 LINES;";

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

if (mysqli_affected_rows($con) == 1) {
  $message = "The data was successfully added!";
} else {
  $message = "The user update failed: ";
  $message .= mysqli_error($con); 
};

echo $message;
mysqli_close($con);

Can you please let me know why this is happening?

Upvotes: 0

Views: 883

Answers (1)

Barmar
Barmar

Reputation: 782498

Your test for success is wrong. mysqli_affected_rows() returns the number of rows that were inserted into the table, which should be the same as the number of lines in the CSV file. I doubt a 40-meg file is only 1 line, so testing this for == 1 is wrong.

If you want to know if the query was successful, test $result.

if ($result) {
    $message = "The data was successfully added!";
} else {
    $message = "The user update failed: " . mysqli_error($con);
}

Upvotes: 2

Related Questions