Generate a random reference code and check it's unique


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. $unique = FALSE;
  2.  
  3. while ($unique == FALSE){
  4.  
  5. $ref = md5(date('r', time()) . rand(0,100));
  6.  
  7. $check_ref = mysql_query("SELECT ref FROM table_name WHERE ref = '$ref'");
  8.  
  9. if(mysql_num_rows($check_ref)==0){
  10.  
  11. $unique = TRUE;
  12.  
  13. }
  14.  
  15. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.