How do I put extra number at the end of username if duplicate

前端 未结 8 1983
醉话见心
醉话见心 2021-01-19 23:31

Hard to explain, and let me show an example.

  1. If username foo already exists in MySQL I want php script will allow it but to be foo1
8条回答
  •  遇见更好的自我
    2021-01-20 00:17

    Try this one (you need to modifiy this to your needs!)

    $username_loop = $username = "foo"; // edit: compatibility to usernames n.e. "foo"
    $count = 0;
    while (){
      $count++;
      $query =  "SELECT username FROM User WHERE username = ".$username_loop;
      $result = mysql_query($query);
      if ($result){
         $username_loop = $username.$count;
      }
      else break;
    }
    

提交回复
热议问题