How can you assign values to user variables to skip unwanted columns during data import using LOAD DATA INFILE in PHP?
When importing data using LOAD DATA INFILE in PHP, you can assign values to user variables to skip unwanted columns by simply not assigning those columns to any variable. This allows you to only import the columns you need and skip the ones you don't want.
// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);
// Import data skipping unwanted columns
$sql = "LOAD DATA INFILE 'data.csv'
INTO TABLE tablename
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
(@col1, @col2, @col3)
SET desired_column1 = @col1, desired_column2 = @col2, desired_column3 = @col3";
$conn->query($sql);
// Close the connection
$conn->close();
Keywords
Related Questions
- What are the potential pitfalls of not passing the database connection as a parameter to functions in PHP?
- What are the recommended alternatives to the mysql extension in PHP for database communication?
- What are the benefits of using a dedicated mailer class like PHPMailer or Swift Mailer for sending emails in PHP, as suggested by forum participants?