Jump to content

simple for loop issue


Drongo_III

Recommended Posts

I am trying to recursively pull records from a database to write to a csv.

 

There are three test rows of data in my table (and no not including the table headers).

 

The only issue is i can’t seem to get my for loop to display all the records – it only displays the last 2 rows so  i’m very confused.

Anyone suggest why this isn’t working?

$num_rows = mysql_num_rows($export);

		for ($i=0; $i <= $num_rows; $i++ ) {

		$row = mysql_fetch_row($export);
		echo $row[1] . $row[2] . $row[3] . $row[4] . $row[5] .  $row[6] ."<br/>";
		echo $i;



		}

 

Incidentally the query is just a fetch all from datasbase.

 

Anyone see where i'm going wrong?

 

Thanks,

 

drongo

Link to comment
Share on other sites

Best guess is that in the code that is before the code you did post, you are fetching and discarding the first row from the result set. Posting all the code from the query through to the end of your loop is the best way of getting the quickest solution.

 

The code you did post is also producing errors on the last pass through the loop because of the equal comparison in the loop condition (and that you are starting the loop at zero.) You should only be using less-than <

 

You should also have php's error_reporting set to E_ALL and display_errors set to ON so that php will help you by reporting and displaying all the errors it detects.

Link to comment
Share on other sites

Hi PFM

 

Thanks for the spots there. I will sort the issues with the loop now and starting getting into the habit of turning all errors on too.

 

I found an unnecessary fix for the problem in using:

 

mysql_data_seek($export, 0) ;

 

And after you mentioned the code before is likely causing the issue i had a realisation! I had already called mysql_fetch_row further up in my code. So i am guessing that was pushing the database pointer on one record which is why the first record was always getting missed by the loop. For some reason i assumed that a new call to the fetch the row would reset the pointer but thinking about it that would probably be very silly indeed! 

 

Thanks for your advice. Very grateful.

 

Drongo

 

 

 

Best guess is that in the code that is before the code you did post, you are fetching and discarding the first row from the result set. Posting all the code from the query through to the end of your loop is the best way of getting the quickest solution.

 

The code you did post is also producing errors on the last pass through the loop because of the equal comparison in the loop condition (and that you are starting the loop at zero.) You should only be using less-than <

 

You should also have php's error_reporting set to E_ALL and display_errors set to ON so that php will help you by reporting and displaying all the errors it detects.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.