Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

Exportando tabela mysql para arquivo .txt ou .doc usando PHP

<?php
    $fh = fopen('data.txt', 'w');
    $con = mysql_connect("localhost","root","");
    mysql_select_db("db_name", $con);

    /* insert field values into data.txt */

    $result = mysql_query("SELECT * FROM table_name");   
    while ($row = mysql_fetch_array($result)) {          
        $last = end($row);          
        $num = mysql_num_fields($result) ;    
        for($i = 0; $i < $num; $i++) {            
            fwrite($fh, $row[$i]);                      
            if ($row[$i] != $last)
               fwrite($fh, ", ");
        }                                                                 
        fwrite($fh, "\n");
    }
    fclose($fh);
?>