Is it advisable to store email addresses directly in a database instead of a text file in PHP applications?

It is generally advisable to store email addresses directly in a database instead of a text file in PHP applications. Storing email addresses in a database allows for easier management, querying, and data integrity. It also provides better security measures to protect sensitive information.

// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

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

// Insert email address into the database
$email = "example@email.com";
$sql = "INSERT INTO email_addresses (email) VALUES ('$email')";

if ($conn->query($sql) === TRUE) {
    echo "Email address added successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();