Hi,
I'm trying to use foreach to write the results of a query to a csv file (fputcsv is not an option as my server is on php 4.7.4).
Problem is that the second foreach returns ONLY the first row of the result set and repeats that row for the duration of the row count; so i get 65 repetitive lines...
Been all over the net, but cannot find a solution to this problem.
Thx for any help or suggestions you cann give me!
Here's the code:
I'm trying to use foreach to write the results of a query to a csv file (fputcsv is not an option as my server is on php 4.7.4).
Problem is that the second foreach returns ONLY the first row of the result set and repeats that row for the duration of the row count; so i get 65 repetitive lines...
Been all over the net, but cannot find a solution to this problem.
Thx for any help or suggestions you cann give me!
Here's the code:
Code:
$query = "SELECT * FROM who_ledenlijst, who_contributie, who_contrib_data WHERE who_ledenlijst.lidnr = who_contributie.lidnr AND who_contributie.periode_id = who_contrib_data.periode_id AND betaald = 'N' ORDER BY $sorteer $sort"; $csvresult = mysql_query($query); $csvrow = mysql_fetch_array($csvresult, MYSQL_ASSOC); $numrows = mysql_num_rows($csvresult); echo "Aantal rijen in de query: $numrows<br />"; // maak een downloadbaar databestand van de query $file = "data.txt"; if (file_exists($file)) unlink($file); //delete oud bestand indien aanwezig $fh = fopen($file, 'a') or die ("Kan bestand niet aanmaken"); foreach($csvrow as $name => $value) { $name = "$name,"; fwrite($fh, $name); } fwrite($fh, "\n"); for ($i=0;$i<=$numrows;$i++) { foreach($csvrow as $value) { $value = "$value,"; fwrite($fh, $value); //echo $value; } fwrite($fh, "\n"); } fclose($fh);
Comment