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

前端 未结 8 1885
醉话见心
醉话见心 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:02

    First, check if the desired username exists in the database:

    SELECT COUNT(*) AS numRows FROM table 
    WHERE SUBSTRING(username, 1, CHAR_LENGTH(desiredUserName)) = desiredUserName
    

    If numRows is 0, you can leave the username as it is. If it is 1 you can just add '1' to the end of the username. It gets a bit more complicated if numRows > 1. You would need to get the current maxmium number which appends that user name:

    SELECT CAST(SUBSTRING(username,(CHAR_LENGTH(desiredUserName) + 1)) AS SIGNED) AS maxNum
    FROM table
    WHERE SUBSTRING(username,1,CHAR_LENGTH(desiredUserName)) = desiredUserName
    ORDER BY CAST(SUBSTRING(username,(CHAR_LENGTH(desiredUserName) + 1)) AS SIGNED) DESC
    LIMIT 1
    

    Now you just need to add 1 to the returned maxNum and append that to the desired username

    0 讨论(0)
  • 2021-01-20 00:06

    First, use a regular expression to parse out the leading alpha part of the requested username, together with any trailing numeric suffix, something like:

    ^([a-zA-Z_]+)(\d)*$
    

    The first group gives you the stem (let's assign this to a variable called UsernameStem) and the second group gives you the number (let's assign this to UsernameNumber). Beware that the user may have not entered a numeric suffix, so you need to check for this special case and assign 0 to UsernameNumber.

    You can then issue a SQL statement on your users table as follows:

    SELECT MAX(username) FROM users WHERE username LIKE :UsernameStem || '%' 
    

    This will give you the existing username with the highest numeric suffix for the user's desired name.

    You can again use the regex above to parse out this maximum number - this time examine the second group returned by your regex match. You can then add 1 to this number to give the next available username.

    Example: the user request "foo21". The regex will parse out "foo" as the UsernameStem and "21" as the username number. Assuming that you have a user in your database with the username "foo44", the SQL query will return you this username. Applying the regex again to this username from your database will enable you to parse out the number "44". You can then convert this to an int, add 1, then concatenate this with your original UsernameStem. This will yield "foo45".

    Good luck!

    0 讨论(0)
  • 2021-01-20 00:09

    I would use something like this:

    // the username
    $username = "foo";
    
    // find all users like foo%
    $q = mysql_query("SELECT * FROM users WHERE username LIKE '".$username."%' ORDER BY id DESC");
    
    // no users
    if (mysql_num_rows($q) == 0) {
        // create $username
    }
    else {
    
        $last_num = 0;
    
        // find all foo users
        while ($row = mysql_fetch_array($q)) {
    
            // match foo + number
            if (preg_match("/^".preg_quote($username)."+([0-9]+)$/i", $row['username'], $match)) {
    
                // extract the number part
                $num = (int) $match[1];
    
                // set the highest number
                if ($num > $last_num) $last_num = $num;
            }
        }
    
        // append the highest number to the username
        if ($last_num > 0) $username .= $last_num + 1;
    
        // create $username
    }
    
    0 讨论(0)
  • 2021-01-20 00:12

    Some variation of

    $number = 1;
    $query = "select name from users where username = ".$username.$number;
    $res = mysql_query($query);
    $num_rows = mysql_num_rows($res);
    while ($num_rows) {
     $number ++;
     $query = "select name from users where username = ".$username.$number;
     $res = mysql_query($query);
     $num_rows = mysql_num_rows($res);
    }
    

    The above will increment the number until one is found that is not currently in the database.

    You will obviously have to edit the values to suit your setup

    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
  • 2021-01-20 00:24
        while (exists($username)) {
            if (!is_numeric(substr($username, -1, 1))) {
                $username = "{$username}0";
            }
            $username++;
        }
    
        return $username;
    

    You get: test, test1, test2 and so on

    0 讨论(0)
提交回复
热议问题