Q:

while($row = mysql_fetch_array($query)) doesn't work in second time

I have:
$query = mysql_query("SELECT ... ");
while($row = mysql_fetch_array($query)) { first time used; }
...
while($row = mysql_fetch_array($query)) { second time used; }

In second time it doesn't work. Why?

A:

That's because the internal data pointer has reached its end. To reread the query, either rewind the pointer with mysql_data_seek(), or reissue the query.

A:

A MySQL result resource has an internal pointer, much like a PHP array, and when you have run through it once, the pointer is at the end. You can reset the data pointer using mysql_data_seek():
while ($row = mysql_fetch_array($query)) {
  // First time used
}
mysql_data_seek($query, 0);
while ($row = mysql_fetch_array($query)) {
  // Second time used
}

...but what is arguably a better/more "standard" approach is to run through the pointer once, store the results in a temporary array and then you can release the memory used by the results and loop the data as an array:
// Do the query
$query = mysql_query("SELECT ... ");

// Cache the results in an array
$results = array();
while ($row = mysql_fetch_array($query)) {
  $results[] = $row;
}

// Free the result resource
mysql_free_result($query);

foreach ($results as $row) {
  // First time used
}
foreach ($results as $row) {
  // Second time used
}

As a side note, it is more resource efficient to use mysql_fetch_assoc() or mysql_fetch_row() - almost every use case only calls for mysql_fetch_assoc(), it is very uncommon that you indexed keys for a MySQL result, and even less common that you need both indexed and associative.

