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

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

提交回复
热议问题