A helpful note about the 'resource' data type.
Since the 'resource' variable is pointing to a row in a result set at any given time, you can think of it as being passed to this function by reference every time you pass it or assign it to a variable.
<?
$sql = "SELECT * from <table>";
$result = mysql_query($sql);
$temp_result = $result;
while ($row = mysql_fetch_assoc($temp_result)) {
// do stuff with $row
}
while ($row = mysql_fetch_assoc($result)) {
// This code will never run because the 'resource' variable is pointing past the end of the result set,
// even though it was *not* assigned by reference to $result2.
}
?>
Therefore, the following snipits are functionally identical:
<?
// Start snipit 1
$sql = "SELECT * from <table>";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
// do stuff with $row
}
mysql_data_seek($result, 0);
while ($row = mysql_fetch_assoc($result)) {
// do other stuff with $row
}
// Start snipit 2
$sql = "SELECT * from <table>";
$result = mysql_query($sql);
$temp_result = $result;
while ($row = mysql_fetch_assoc($temp_result)) {
// do stuff with $row
}
mysql_data_seek($result, 0);
while ($row = mysql_fetch_assoc($temp_result)) {
// do other stuff with $row
}
?>