How can PHP be used to import XML data into a MySQL database efficiently?

To efficiently import XML data into a MySQL database using PHP, you can utilize the SimpleXML extension to parse the XML file and extract the necessary data. Then, establish a connection to the MySQL database using mysqli or PDO, and insert the extracted data into the appropriate tables.

<?php
// Load the XML file
$xml = simplexml_load_file('data.xml');

// Connect to the MySQL database
$mysqli = new mysqli('localhost', 'username', 'password', 'database');

// Loop through the XML data and insert into the database
foreach ($xml->data as $data) {
    $value1 = $mysqli->real_escape_string($data->value1);
    $value2 = $mysqli->real_escape_string($data->value2);
    
    $query = "INSERT INTO table_name (column1, column2) VALUES ('$value1', '$value2')";
    $mysqli->query($query);
}

// Close the database connection
$mysqli->close();
?>