What are the advantages of using CSV or SQL files over Excel files for database imports?

When importing data into a database, using CSV or SQL files over Excel files can offer several advantages. CSV files are lightweight and easy to work with, making them more efficient for bulk imports. SQL files allow for direct execution of queries, which can be faster and more reliable than manually copying and pasting data from an Excel file. Additionally, CSV and SQL files are more compatible with different database systems, making them a more versatile choice for importing data.

// Import data from a CSV file into a MySQL database

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Path to the CSV file
$csvFile = 'data.csv';

// Open the CSV file
$file = fopen($csvFile, 'r');

// Loop through the CSV file and insert data into the database
while (($data = fgetcsv($file, 1000, ',')) !== FALSE) {
    $sql = "INSERT INTO table_name (column1, column2, column3) VALUES ('$data[0]', '$data[1]', '$data[2]')";
    if ($conn->query($sql) === TRUE) {
        echo "Record inserted successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
}

// Close the file and the database connection
fclose($file);
$conn->close();