How can PHP be used to read and sort the content of a .txt file for insertion into a SQL database?
To read and sort the content of a .txt file for insertion into a SQL database using PHP, you can first read the file line by line, sort the content as needed, and then insert it into the database using SQL queries. You can use functions like file(), explode(), and sort() to read, manipulate, and sort the content before inserting it into the database.
<?php
// Read the content of the text file
$lines = file('data.txt');
// Sort the content alphabetically
sort($lines);
// Connect to the database
$pdo = new PDO('mysql:host=localhost;dbname=database', 'username', 'password');
// Insert each line into the database
foreach($lines as $line) {
$stmt = $pdo->prepare("INSERT INTO table_name (column_name) VALUES (:line)");
$stmt->bindParam(':line', $line);
$stmt->execute();
}
?>