Forum Moderators: coopster
mysql_select_db($database_sql_connection, $sql_connection);
$query_collection = "SELECT * FROM tblCDCollection ORDER BY artist, year_released, title";
$check_collection = mysql_query($query_collection, $sql_connection) or die(mysql_error());
$row_collection = mysql_fetch_assoc($check_collection);
$totalRows_collection = mysql_num_rows($check_collection);
while ($row_collection = mysql_fetch_assoc($check_collection))
{
echo $row_collection['ID'];
echo ("<br>");
}
you need to reset the internal pointer after calling mysql_num_rows
...
$row_collection = mysql_fetch_assoc($check_collection); // whats this for?
$totalRows_collection = mysql_num_rows($check_collection);
mysql_data_seek($check_collection,0);
while ($row_collection = mysql_fetch_assoc($check_collection)) {
echo $row_collection['ID'];
echo ("<br>");
}
you need to reset the internal pointer after calling mysql_num_rows
When I first read this I thought jatar_k meant that whenever you use mysql_num_rows() you need to reset the internal row pointer with a data_seek. Then I thought, "hey, wait a minute, I never do that".
Then I realized that it is only because of the extra fetch caught there by jatar_k that the mysql_data_seek is required. mysql_fetch_assoc() will advance the internal row pointer within the result set, but mysql_num_rows() does not. This statement made solely on testing, I cannot find any definitive documentation that supports the theory. Anyone else have any input here?
$row_collection = mysql_fetch_assoc($check_collection);
will net the user what they want. you're pulling one set of results, then immediately doing that again in the while statement.
the alternative of that (in case you need to examine a first result) is to use a do... while loop. for example:
mysql_select_db($database_sql_connection, $sql_connection);
$query_collection = "SELECT * FROM tblCDCollection ORDER BY artist, year_released, title";
$check_collection = mysql_query($query_collection, $sql_connection) or die(mysql_error());
$row_collection = mysql_fetch_assoc($check_collection);
$totalRows_collection = mysql_num_rows($check_collection);
do {
echo $row_collection['ID'];
echo ("<br>");
}while($row_collection = mysql_fetch_assoc($check_collection))
but in this case, that's kinda silly unless there's more to the code than what's being shown here. do..while loops are only really wise unless you know for a fact that either you have data to use, or you're positive that you dont care and want at least 1 cycle through the loop.
From MySQL [dev.mysql.com]:
Once you have called mysql_store_result() and got a result back that isn't a null pointer, you may call mysql_num_rows() to find out how many rows are in the result set.
Nothing in the APIs regarding resetting the internal row pointer before doing so, at least not that I can find...