How can PHP be used to automate the import of CSV tables in phpMyAdmin?
To automate the import of CSV tables in phpMyAdmin using PHP, you can use the `LOAD DATA INFILE` SQL statement. This statement allows you to load data from a CSV file directly into a MySQL table. You can use PHP to execute this SQL statement and automate the import process.
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";
$csv_file = "path/to/csv/file.csv";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "LOAD DATA INFILE '$csv_file' 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 importing CSV data: " . $conn->error;
}
$conn->close();
?>