Try analysing the result before fetching it.
If result is empty, skip fetching.
$result = mysql_query("SELECT * FROM table1");
if (!$result || !mysql_num_rows($result)) {
die('Empty set.');
}
while ($row = mysql_fetch_array($result)) {
// Your code here
}