简体   繁体   English

MySQL自动增量问题

[英]Mysql auto increment issue

 function randomUnique(){    
        return $randomString =rand(0, 9999); //generate random key    
    }

    function insert($uid,$name,$email){
            $link = mysqli_connect("localhost", "root", "", "dummy");        
             $query = "insert into `usertbl`(`uid`,`name`,`email`) 
                       values('".$uid."','".$name."','".$email."');";
            if(mysqli_query($link, $query)){        
                return $rval = 1;
            }else if(mysqli_errno($link) == 1062){          
                insert(randomUnique(),$name,$email);
            }else if(mysqli_errno($link != 1062)){          
               return $rval = 2;// unsuccessful query           
            }
        }


        $uid = randomUnique();
        $name = "sam";
        $email = "sam@domain.com";
        $msg_code = insert ($uid,$name,$email);
        echo $msg_code;

I have 4 columns in the table : id (PK AI), uid (varchar unique), name (varchar), email (varchar). 我在表中有4列: id (PK AI), uid (varchar唯一), name (varchar), email (varchar)。 When I want to create a new user entry.A random key is generated using the function 'randomUnique()'.And I have the column 'id' set to AI so it tries to input the details, but if the key repeats that error number 1062 is returned back from mysql.Everything runs well except for id column which is set to AI. 当我想创建一个新的用户条目时,使用函数'randomUnique()'生成一个随机密钥,并且我将'id'列设置为AI,因此它将尝试输入详细信息,但是如果密钥重复该错误从MySQL返回了1062号。除了id列设置为AI之外,其他所有程序都运行良好。 the column value is skipped once if one key is a duplicate. 如果一个键重复,则该列值将被跳过一次。 The above code is a recursive function so the number of values skipped in column 'id' is directly proportional to the number of times the function is called. 上面的代码是一个递归函数,因此在“ id”列中跳过的值数与该函数被调用的次数成正比。

Example: 例:

id | uid  |   name |   email
1  |  438 |   dan  |  dan@domail.com
2  | 3688 |  nick  | nick@domain.com
4  | 410  |  sid   | sid@domain.com

Here, we can see number 3 has skipped bcoz either random number function gave us a number 438 or 3688 which tends to throw back an error and our recursive function repeats once skipping the number 3 and entering 4 next time on successful execution. 在这里,我们可以看到数字3跳过了bcoz,或者随机数函数给了我们数字438或3688,这往往会抛出错误,并且一旦成功跳过数字3并下次输入4,我们的递归函数就会重复执行。

I need to fix the auto increment so it enters the value into proper sequence . 我需要修复自动增量,以便它按正确的顺序输入值。 I cannot change the structure of the table. 我无法更改表格的结构。

You can check whether an entry already exists with that uid before performing the INSERT operation, eg 您可以在执行INSERT操作之前检查该uid是否已经存在一个条目,例如

SELECT COUNT(*) FROM table WHERE uid = '$uid';

This will return you the count of records that have the newly generated uid. 这将返回具有新生成的uid的记录count You can check this count and perform the INSERT only if count is 0. If not, you can call the function again to generate anoter random value. 您可以检查此count并仅在count为0时执行INSERT否则,可以再次调用该function以生成注释随机值。

In each function calling you are creating new db link, may be for this situation php provided mysqli_close($link); 在每个要调用的函数中,您正在创建新的数据库链接,可能是针对这种情况的php提供了mysqli_close($link);

Either you close connection 您要么关闭连接

  if(mysqli_query($link, $query)){        
            return $rval = 1;
        }else if(mysqli_errno($link) == 1062){
            mysqli_close($link);          
            insert(randomUnique(),$name,$email);
        }else if(mysqli_errno($link != 1062)){          
           return $rval = 2;// unsuccessful query           
        }

OR simply put DB connection out of function 或者只是简单地使数据库连接失效

$link = mysqli_connect("localhost", "root", "", "dummy");   
function insert($uid,$name,$email){

Use PHP's uniqid function, it generates a proper unique is. 使用PHP的uniqid函数,它会生成一个适当的唯一值。

http://php.net/manual/en/function.uniqid.php http://php.net/manual/zh/function.uniqid.php

What is this is being used for? 这是做什么用的? You may be able to use the id column which will perform much faster and is already guaranteed to be unique. 您也许可以使用id列,该列的执行速度会更快,并且已经保证是唯一的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM