Soup Posted October 14, 2009 Share Posted October 14, 2009 I'm trying to create a unique 8 digit id and have it check against the database so there can be no duplicate. It stores the id fine but the check aspect doesn't seem to be functioning since i have gotten the same number twice on test runs. Here is what i have so far, any help would be great. $loop=true; while($loop) { $rand = rand(10000000,99999999); if(mysql_query("SELECT * FROM $table_name WHERE `rand` = '{$rand}' LIMIT 1")) $loop=false; } $query ="INSERT into $table_name (id, rand) values ('id','$rand')"; if (mysql_db_query ($db, $query)) { print ("Your information was successfully uploaded!<br />\n"); } else { print ("Sorry! There was an error in uploading your information!<br />\n"); } mysql_close ($link); ?> <html> <head></head> <body> <?php echo $rand; ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/177665-checking-uniqueness-against-database/ Share on other sites More sharing options...
PFMaBiSmAd Posted October 14, 2009 Share Posted October 14, 2009 if(mysql_query("SELECT * FROM $table_name WHERE `rand` = '{$rand}' LIMIT 1")) The above line of code just tests if the query executed without errors. It does not test if any row(s) were returned in the result set. You need to use mysql_num_rows() to find out how many rows are in a result set. Quote Link to comment https://forums.phpfreaks.com/topic/177665-checking-uniqueness-against-database/#findComment-936786 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.