I have a text file with many emails and I want to save everything in a database, how do I get each email from the file in PHP?
Emails are comma-separated.
I have a text file with many emails and I want to save everything in a database, how do I get each email from the file in PHP?
Emails are comma-separated.
Starting from the presumption that the database has the e-mail table, and that the file is teste.txt
:
// Pega o conteudo do arquivo teste.txt
$arquivo_texto = file_get_contents('teste.txt');
// Cria um array com os emails do arquivo teste.txt
$emails = explode(',', $arquivo_texto);
// Abre uma conexão com o banco de dados mysql
$con = new PDO("mysql:host=localhost;dbname=nome_do_banco", "root", "");
// Loop para inserção no banco de dados
foreach ($emails as $value) {
$rs = $con->query("INSERT INTO emails VALUES ?");
$rs->bindParam(1, $value);
$rs->execute();
}