First of all, a do{...}while() loop would be tidier than writing most of the code twice. (The inner while loop ought probably be a for(){} loop, too.)
Secondly, you shouldn't keep reseeding the random number generator (in fact, for many years the only reason seeding the generator would be necessary is if you wanted repeatable sequences).
Third, there is an easier way to restrict the output of [man]rand/man to a certain range.
Fourth, you can access a single character of a string using $string[$position] syntax.
Fifth, if you want to find out if a record exists in the database but you don't care about the information, "SELECT COUNT(*) FROM ..." will return one record with one column that contains the number of matching records (there's an even more efficient method using EXISTS).
Sixth, your argument to mysql_num_rows() is wrong.