Drongo_III Posted November 25, 2011 Share Posted November 25, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/251793-simple-for-loop-issue/ Share on other sites More sharing options...
PFMaBiSmAd Posted November 25, 2011 Share Posted November 25, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/251793-simple-for-loop-issue/#findComment-1291153 Share on other sites More sharing options...
Drongo_III Posted November 25, 2011 Author Share Posted November 25, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/251793-simple-for-loop-issue/#findComment-1291159 Share on other sites More sharing options...
Pikachu2000 Posted November 25, 2011 Share Posted November 25, 2011 Why not just remove the first call to mysql_fetch_row()? Quote Link to comment https://forums.phpfreaks.com/topic/251793-simple-for-loop-issue/#findComment-1291160 Share on other sites More sharing options...
Drongo_III Posted November 25, 2011 Author Share Posted November 25, 2011 I did I was just trying to explain the process of how i came the realisation which for me was quite gratifying hehe! Why not just remove the first call to mysql_fetch_row()? Quote Link to comment https://forums.phpfreaks.com/topic/251793-simple-for-loop-issue/#findComment-1291161 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.