我想根据名字和姓氏自动生成用户名。例如,
如果firstname = John
和lastname = Tory
,则用户名应为johntory
。但是,如果该用户名已经存在于数据库中,我希望将其增加1表示使用相同的名称,下一个用户名将为johntory2
。
我在PHP
..
foreach($existing_users as $u){
if($u['username'] != $new_generated_username){
// Enter it directly...
}
else{
// increment count value...
}
}
但是,如果用户名是johntory5
或者其他什么,那么就会出现问题...
有什么方法可以通过检查所有东西直接在mysql的INSERT
语句中更新。
答案 0 :(得分:2)
在PHP中使用循环的简单方法:
<?
$con=mysqli_connect("localhost","user","pass","database");
$username = mysqli_real_escape_string($con,'johntory');
$tmp = $username;
$inc = 1;
$valid = false;
// lookup if the username is in use
$qid = mysqli_query($con,"select id from User where username='$username'");
if(mysqli_num_rows($qid) == 0) $valid = true;
// if it is in use, keep looking up until a valid username is found
while(!$valid) {
$username = $tmp . $inc; // append number
$qid = mysqli_query($con,"select id from User where username='$username'");
if(mysqli_num_rows($qid) == 0) $valid = true;
$inc++;
//optional
if($inc>1000) die("Too many John Tory's");
}
// insert user
mysqli_query($con,"insert into User(username) values ('$username')");
?>
您可能希望在用户名上添加索引以加快查询速度。
答案 1 :(得分:2)
您可以直接在INSERT语句中直接执行此操作,如下所示:
INSERT INTO User (username, first, last)
SELECT CONCAT('johntory', COUNT(id)), 'John', 'Tory'
FROM User WHERE username LIKE 'johntory%';
或者如果你不喜欢前面的零,你可以使用它:
INSERT INTO User (username, first, last)
SELECT CONCAT('johntory', CASE WHEN COUNT(id) = 0 THEN '' ELSE COUNT(id) END), 'John', 'Tory'
FROM User WHERE username LIKE 'johntory%';
这非常快速有效。它使用子查询来准备值,然后插入它们。
此外,这将比此处提供的任何PHP解决方案快许多倍,特别是如果您处理数十亿行。
就个人而言,我喜欢MySql的简洁性和性能
享受!
〜Techdude
答案 2 :(得分:1)
我会为用户提供一个表,其中每行保留该用户名的下一个建议编号。
然后用户注册功能如下所示:
class User {
private $firstName;
private $lastName;
public function __construct($firstName, $lastName) {
$this->firstName = $firstName;
$this->lastName = $lastName;
}
public function register(Db $db) {
$username = $this->firstName . $this->lastName;
$query = "SELECT next_suggestion_id FROM user WHERE username = '$username'";
$rows = $db->execute($query);
if (!empty($rows)) {
//user found, incrementing next suggestion id
$newSuggestionId = $rows[0]['suggestion_id'] + 1;
$query = "INSERT INTO user SET suggestion_id = $newSuggestionId"
. " WHERE username = '$username'";
$db->execute($query);
$username .= $newSuggestionId;
}
//inserting user in db
$query = "INSERT INTO user SET username = '$username' /* other columns */";
$db->execute($query);
}
}
答案 3 :(得分:0)
您可以尝试使用连接的第一个和最后一个名称生成用户名。然后使用MySql中的LIKE
子句查找任何匹配的用户名。这将生成一个与自动生成的用户名完全匹配的用户名数组。然后,您可以遍历此数组,直到您自动生成数据库中不存在的用户名。
在PHP中看起来像这样。
// The $suffix is the number to add if the username exists. The default is NULL/nothing
$suffix = null;
// Auto-generate the username.
$username = generate_username_function($firstname, $lastname, $suffix);
// Instantiate a new PDO instance.
$pdo = new PDO('mysql:host=...', $db_username, $db_password); // Insert your DNS string here.
// Build the SQL query.
$query = "
SELECT username
FROM users
WHERE username LIKE ':username%'
";
$statement = $pdo->prepare($query);
$statement->bindParam(':username', $username, PDO::PARAM_STR);
if(!$statement->execute()) {
// throw an exception if the query couldn't be executed.
}
$existing = $statement->fetchAll(PDO::FETCH_ASSOC);
// Loop through the existing usernames to check if the auto-generated username exists.
// Set an $available flag on indicate when the username is available.
$available = false;
$current = 1;
$ceiling = 1000; // This is to avoid infinite loops. Increase this if necessary.
while(!$available) {
// Check if the loop ceiling has been reached.
if($current == $ceiling) {
// throw an exception(preferred) or emit an error.
}
if(!in_array($username, $existing)) {
$available = true;
}else{
if(is_null($suffix)) {
$suffix = 1;
}else{
$suffix++;
}
$username = generate_username_function($firstname, $lastname, $suffix);
}
$current++;
}
这将继续增加后缀,直到找到可用的用户名。用户名的生成可以改进,但我会留给你:D
现在您有了一个可用的用户名,您可以将其与任何其他信息一起存储在数据库中。
最好的问候。