Exports database query to txt with php

Asked

Viewed 294 times

-1

//Arquivo txt
$arquivo = "email.txt";

//abrir arquivo txt 

$arq = fopen($arquivo,"w");

//faz consulta no banco de dados

$result = mysql_query("SELECT * FROM usuarios");

$cabecalho = "Emails extraidos\n";

fwrite($arq, $cabecalho);


while($escrever = mysql_fetch_array($result)){
    $conteudo = $escrever ['email'];

    //escreve no arquivo txt

    fwrite($arq,$conteudo);
}

//fecha o arquivo
fclose($arq);

Mistake you’re making inserir a descrição da imagem aqui

  • Where you connect to your database?

  • $dsn = "mysql:dbname=blog;host=127.0.0.1"; $dbuser = "root"; $dbpass = ""; Try{ $Pdo = new PDO($dsn, $dbuser, $dbpass); }catch(Pdoexception $e){ echo "Connection failed: ". $e->getMessage(); }

  • tried with mysli_query ?

  • the function mysql already deprecated, from a look at the answers of this other question, can help you: https://stackoverflow.com/questions/25644318/deprecated-mysql-query

  • 1
  • with mysqli_query ERROR: Warning: mysqli_query() expects at least 2 Parameters, 1 Given in D: wamp www pagina lan page config.php on line 24

  • how are you utilizing the wamp it is likely that you are using the version PHP7.1 in which functions mysql were discontinued: here is another user with the same problem : https://answall.com/questions/173037/como-resolver-o-erro-call-to-undefined-function-mysql-connect

  • I decided here guys thank you all for your help.

Show 4 more comments

1 answer

1

Solved
Code Working

//Conectar na banco
$link = mysqli_connect("host", "user", "senha", "nome do banco");

//Arquivo txt
$arquivo = "email.txt";

//abrir arquivo txt 
$arq = fopen($arquivo,"w");

//faz consulta no banco de dados
$result = mysqli_query($link, "SELECT * FROM usuarios");

$cabecalho = "Emails extraidos\n";

fwrite($arq, $cabecalho);


while($escrever = mysqli_fetch_array($result)){
    $conteudo = $escrever ['email']."\n";

    //escreve no arquivo txt

    fwrite($arq,$conteudo);
}

//fecha o arquivo
fclose($arq);
  • If possible a brief explanation about how you solved the problem in this way other users can also find the solution.

  • I added: $link = mysqli_connect("host", "user", "password", "database name"); I changed mysql by mysqli and put $link in mysqli_query: mysqli_query($link, "SELECT * FROM users");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.