PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
At the bottom I would think that $i would create a new line every time 2 tables get posted in a row, but it is not working correctly. I have used this code before but cannot figure out why it won't work now.
actually, if I'm reading it correctly, $i should increment every time you go through the while loop. The "$i % 2" isn't actually storing the result in $i, so $i just keeps going up. Also, $i is never reset within the while loop, so...
Not that it really matters. Even if $i was 501... 501 mod 2 is still 1, and 502 mod 2 is still 0.
Typer85 wrote:Please be advised that the resource result that you pass to this function can be thought of as being passed by reference because a resource is simply a pointer to a memory location.
Because of this, you can not loop through a resource result twice in the same script before resetting the pointer back to the start position.
<?php
// Assume We Already Queried Our Database.
// Loop Through Result Set.
while( $queryContent = mysql_fetch_row( $queryResult ) {
// Display.
echo $queryContent[ 0 ];
}
// We looped through the resource result already so the
// the pointer is no longer pointing at any rows.
// If we decide to loop through the same resource result
// again, the function will always return false because it
// will assume there are no more rows.
// So the following code, if executed after the previous code
// segment will not work.
while( $queryContent = mysql_fetch_row( $queryResult ) {
// Display.
echo $queryContent[ 0 ];
}
// Because $queryContent is now equal to FALSE, the loop
// will not be entered.
?>
----------------
The only solution to this is to reset the pointer to make it point at the first row again before the second code segment, so now the complete code will look as follows:
Of course you would have to do extra checks to make sure that the number of rows in the result is not 0 or else mysql_data_seek itself will return false and an error will be raised.
Also please note that this applies to all functions that fetch result sets, including mysql_fetch_row, mysql_fetch_assos, and mysql_fetch_array.
So... your loop isn't working for the above reason (I think).
Edit: actually, now that I read this again... maybe not. Hmm. *looks around for a different answer*.