mysql - PHP: how to convert SQL result in csv -


i'm trying convert sql query result in csv file can't find working solution. have suggestion?

tnx in advance!

here code:

 <? mysql_query("set character set 'utf8'");                    $result3 = mysql_query("select * richieste group mail"); while( $row2 = mysql_fetch_array( $result3 ) )              {              $id="{$row2['id']}";             $nominativo="{$row2['nominativo']}";             $mail="{$row2['mail']}";             $telefono="{$row2['telefono']}";             $comune="{$row2['comune']}";             $cap="{$row2['cap']}";             $provincia="{$row2['provincia']}";             $regione="{$row2['regione']}";               echo"             <tr><td width='20%' align='left' class='testo_verde_maiuscolo'><a href='visualizza_richieste.php?mail=$mail&id=$id'>$nominativo</a></td><td width='15%' align='left' class='testo_verde'><a href='mailto:$mail'>$mail</a></td>             <td width='10%' align='left' class='testo_verde'>$telefono</td><td width='15%' align='left' class='testo_verde'>"; 

looking value in table

            $result_comune = mysql_query("select * comuni cod_istat=$comune"); while( $row_comune = mysql_fetch_array( $result_comune ) )             { $comune_traslata="{$row_comune['comune']}";echo"$comune_traslata";             }             echo"</td><td width='10%' align='left' class='testo_verde'>             $cap</td><td width='10%' align='left' class='testo_verde'>     "; 

here i'm looking value in table

        $result_provincia = mysql_query("select * province cod_provincia=$provincia"); while( $row_provincia = mysql_fetch_array( $result_provincia ) )             {              $provincia_traslata="{$row_provincia['provincia']}";             echo"$provincia_traslata";             }             echo"</td>             <td align='left' class='testo_verde'>"; 

same before, looking value in table

            $result_regione = mysql_query("select * regioni cod_regione=$regione"); while( $row_regione = mysql_fetch_array( $result_regione ) )             {              $regione_traslata="{$row_regione['regione']}";          echo"$regione_traslata";             }             echo"</td>             ";               echo"</tr>"; 

first edit: tryed code:

$_csv_data="id;nominativo;mail;telefono\n"; @fwrite( $_fp, $_csv_data); while (list( $id, $nominativo, $mail, $telefono) = mysql_fetch_row($result3)) { $_csv_data = $id.';'.$nominativo.';'.$mail.';'.$telefono."\n"; @fwrite( $_fp, $_csv_data); } @fclose( $_fp );   

but i'm getting firs line of csv.

} ?>    

the repeated mysql_fetch_array calls in html generating while loop consume of rows, leaving internal result pointer @ end when finished. therefore, when try mysql_fetch_array on same result, being @ end of result, not receive (more) rows.

try adding ...

mysql_data_seek($result3, 0); 

... before second/csv generating loop, move internal pointer start of query result.

as side note, please see the manual on mysql_* function usage:

this extension deprecated of php 5.5.0, , removed in future. instead, mysqli or pdo_mysql extension should used [...]


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -