Actually, IF the CertificateNumber column is a key, you could probably just generate the random number within the MySQL query:
INSERT INTO `Certification` (`name`, `CertificateNumber`) VALUES ('Name here', FLOOR(10000000 + RAND() * 89999999) ) ON DUPLICATE KEY UPDATE `CertificateNumber` = FLOOR(10000000 + RAND() * 89999999);
EDIT: Alternatively, to follow your method and do a SELECT each time to make sure:
for( $i=0, $max=50, $rand=mt_rand(10000000, 99999999); $i < $max; $i++,$rand=mt_rand(10000000, 99999999) ) {
$query = 'SELECT TRUE FROM Certification WHERE `CertificateNumber`=' . $rand . ' LIMIT 1';
$exec = mysql_query($query) or die('MySQL Error - ' . mysql_error());
if(mysql_num_rows($exec) > 0) {
if($i == $max)
die('Error - could not generate an unused random number after 50 times, or the script is erraneous.');
else
continue;
} else {
$ins_query = 'INSERT INTO `Certification` (`name`, `CertificateNumber`) VALUES (\'Name here\', ' . $rand . ')';
$ins_exec = mysql_query($query) or die('MySQL Error - ' . mysql_error());
break;
}
}
The above code is untested, but it looks like it should do what you need it to.