foreach returns ONLY the first row of a joined query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • G33W1zz
    New Member
    • Nov 2009
    • 3

    foreach returns ONLY the first row of a joined query

    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:

    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);
    Last edited by Dormilich; Nov 29 '09, 11:09 PM. Reason: Please use [code] tags when posting code
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    is the value of $numrows 65? (that would explain a lot)

    from what I see in your code, you are only using the first row of your result set anyway.

    Comment

    • G33W1zz
      New Member
      • Nov 2009
      • 3

      #3
      Yes, numrows would return 65 in the given query. I don't see an explanation there however, could you please explain?

      Further i don't see why i'm using only the first row (which IS indeed clearly what's happening) , why doesn't the array pointer get moved to the next row in this case?

      Thx, Eric

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        it’s not the array pointer that’s not moving, it’s the resource pointer (line 10)!

        Comment

        • G33W1zz
          New Member
          • Nov 2009
          • 3

          #5
          OK, but isn't that what the 'for' loop is supposed to do? I've tried all sorts of variations (while, do while) and looked at all sorts of foreach examples on the net, but still can't figure out why this isn't working.
          I've moved on to another solution to get the result i need (the csv file), by including the fwrite in a while loop i already use to produce a table to view the results of the query onscreen.
          I'd just like to understand what it is i'm missing here.

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            OK, but isn't that what the 'for' loop is supposed to do?
            nope, the for loop only repeats the row-reading 65 times, nothing more, nothing less. (the count variable $i is used nowhere, in contrast to the "normal" for setup)

            at this place I’d advice to have a look at the description of mysql_fetch_arr ay() (especially at the code examples)

            Comment

            Working...