How to add an additional number at the end of a username if duplicating it

It’s hard to explain, and let me set an example.

  • If the username foo already exists in MySQL, I want the php script to let it be foo1
  • Then if foo1 exists , the script will generate the username foo2
  • If foo2 exists, then foo3

How to do it?

Like Col. Shrapnel said Natural Growth, which seems more reasonable. like "New Folder (3)" on Windows

+3
source share
7 answers

-, , . - :

$name = 'foo';
$first_name = $name;
$i = 0;
do {
  //Check in the database here
  $exists = exists_in_database($name);
  if($exists) {
    $i++;
    $name = $first_name . $i;
  }
}while($exists);
//save $name

- , "foo" , . SQL.

, , , .

+2

, :

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

numRows 0, . 1, "1" . , numRows > 1. maxmium-, :

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

1 maxNum

+2

( !)

$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

- , :

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

( UsernameStem), ( Number). , , , , 0 UsernameNumber.

SQL :

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

.

, - , . 1 , .

: "foo21". "foo" UsernameStem "21" . , "foo44", SQL- . "44". int, 1, UsernameStem. "foo45".

!

0

- :

// 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
  $getname   = $_POST['getname'];

  $i++;

  $q =  "SELECT username FROM User WHERE username LIKE '".$getname."%'";

  $r = mysql_query($q);

  if(mysql_num_rows($r) > 0){

    while($row = mysql_fetch_object($r)){
      if($row->username == $getname){
        $uname = $getname.$i;
      }else if($row->username == ($getname.$i)){
        $uname = $getname.$i+1;
      }
      $i++;
    }

  }
  else
  {
   $uname = "foo";
  }
0

$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 increase the number until one of them is found that is not currently in the database.

You will obviously have to edit the values ​​according to your settings.

-2
source

Source: https://habr.com/ru/post/1781426/


All Articles