What is a potential efficient method for importing data from a text file into a database using PHP?

When importing data from a text file into a database using PHP, one efficient method is to read the contents of the file line by line and then insert each line into the database using SQL queries. This can be achieved by opening the text file, reading it line by line, parsing the data as needed, and then executing SQL INSERT queries to add the data to the database.

<?php

$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);
}

// Open the text file for reading
$filename = "data.txt";
$file = fopen($filename, "r");

// Read the file line by line
while (!feof($file)) {
    $line = fgets($file);
    
    // Parse the data as needed
    $data = explode(",", $line);
    
    // Insert data into the database
    $sql = "INSERT INTO table_name (column1, column2, column3) VALUES ('" . $data[0] . "', '" . $data[1] . "', '" . $data[2] . "')";
    $result = $conn->query($sql);
    
    if (!$result) {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
}

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

?>