What is the significance of using LOAD DATA LOCAL INFILE in PHP for importing CSV files into a database?
Using LOAD DATA LOCAL INFILE in PHP allows for efficient importing of CSV files into a database by directly loading the data from the file into the database table. This method is faster and more resource-efficient compared to manually parsing and inserting each row of data. However, it is important to note that using this method may pose security risks if not handled properly.
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "LOAD DATA LOCAL INFILE 'path/to/file.csv'
INTO TABLE table_name
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
IGNORE 1 LINES";
if ($conn->query($sql) === TRUE) {
echo "CSV data imported successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>