You could use LIMIT - it takes either 1 or 2 arguments. If only one is supplied, then that is the number of records returned, if 2 are supplied, the first is an offset, and the second is the number of records. An example:
for($n=0; $n<($totalnumrows/10); $n++)
{
$query = "SELECT FROM table ORDER BY id LIMIT ($n10), 10";
//do the query and all other code
}
This would select 10 records from the database at a time, i.e, the first loop will select the first 10, the second will have an offset of 10 (1*10) and will return records 11-20 etc.
You're probably just as well off just setting the timeout high though, although this way would allow you to build a kind of progress meter