The obvious problem is that in
while($found = 0) {
You're setting $found to 0 and running the loop only if $found is not false. But it is false. So the loop doesn't run.
Another problem is you're restarting the RNG from scratch with every iteration, instead of starting it once (before the loop). Since time() will return the same value for an entire second, mt_rand() will return the same number over and over again for that whole second: so you'll end up testing numbers at the rate of one per second.
A third problem is that you're looking for $res in the database instead of $lottonum.
But the second two bugs are incidental, becuase the first bug stops those bits of code from ever running anyway.
I think you'll get much better performance if you ask for the lottery numbers that have already been inserted and considering numbers until you've got the one you want instead of asking for everything for one record that has the lottery number you're considering.
$query = mysql_query('select number from lotto');
$lottery_numbers = array();
$query_count=mysql_num_rows($query);
for($i=0; $i<$query_count; $i++)
{ $lottery_numbers[] = mysql_result($query,$i,'number');
}
mt_srand(time());
for($i=0; $i<$amount; $i++)
{ $found = false;
do
{ $lottonum = mt_rand(10000000,99999999);
$found = in_array($lottonum,$lottery_numbers);
} while($found);
//...and insert
}
Unless you're looking at millions of lottery numbers. A stored procedure would be nice for this if MySQL supports such things these days. If no:
$found = false;
mt_srand(time());
for ($i=0;$i<$amount;$i++)
{ while(!$found)
{ $lottonum = mt_rand(10000000,99999999);
$test = mysql_query("select count(*) from lotto where number='".$lottonum."'");
if ($mysql_result($test,0,0))
{ $found = true;
}
}
//...and insert
}