What are the advantages of using LOAD DATA for processing CSV files in MySQL compared to PHP scripts?
When processing large CSV files in MySQL, using the LOAD DATA command is more efficient than using PHP scripts to manually parse and insert each row into the database. This is because LOAD DATA is a native MySQL command optimized for importing data from external files, resulting in faster processing times and reduced server load.
// Assuming the CSV file is named 'data.csv' and has columns 'column1', 'column2', 'column3'
// Connect to MySQL database
$connection = mysqli_connect("localhost", "username", "password", "database");
// Use LOAD DATA command to import CSV file into MySQL table
$query = "LOAD DATA INFILE 'data.csv' INTO TABLE table_name FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' IGNORE 1 LINES (column1, column2, column3)";
mysqli_query($connection, $query);
// Close database connection
mysqli_close($connection);
Keywords
Related Questions
- How can inconsistencies between local and server databases impact the functionality of PHP scripts?
- Are there alternative methods to passing information between PHP scripts besides using the GET method in the URL?
- How can the use of "?" or "& in defining a mail subject impact the functionality in different email clients?