How to generate unique id in MySQL?

前端 未结 16 2619
生来不讨喜
生来不讨喜 2020-11-29 03:17

I\'m programming a script using PHP and MySQL and I want to get a unique id (consisting of a string: capitals and small letters with numbers) like: gHYtUUi5b.

相关标签:
16条回答
  • 2020-11-29 04:02

    For uniqueness what I do is I take the Unix timestamp and append a random string to it and use that.

    0 讨论(0)
  • 2020-11-29 04:06

    I use UUID() to create a unique value.

    example:

    insert into Companies (CompanyID, CompanyName) Values(UUID(), "TestUUID");
    
    0 讨论(0)
  • 2020-11-29 04:06
     <?php
        $hostname_conn = "localhost";
        $database_conn = "user_id";
        $username_conn = "root";
        $password_conn = "";
         $conn = mysql_pconnect($hostname_conn, $username_conn,   $password_conn) or trigger_error(mysql_error(),E_USER_ERROR); 
       mysql_select_db($database_conn,$conn);
       // run an endless loop      
        while(1) {       
        $randomNumber = rand(1, 999999);// generate unique random number               
        $query = "SELECT * FROM tbl_rand WHERE the_number='".mysql_real_escape_string ($randomNumber)."'";  // check if it exists in database   
        $res =mysql_query($query,$conn);       
        $rowCount = mysql_num_rows($res);
         // if not found in the db (it is unique), then insert the unique number into data_base and break out of the loop
        if($rowCount < 1) {
        $con = mysql_connect ("localhost","root");      
        mysql_select_db("user_id", $con);       
        $sql = "insert into tbl_rand(the_number) values('".$randomNumber."')";      
        mysql_query ($sql,$con);        
        mysql_close ($con);
        break;
        }   
    }
      echo "inserted unique number into Data_base. use it as ID";
       ?>
    
    0 讨论(0)
  • 2020-11-29 04:07

    You may like the way that we do it. I wanted a reversible unique code that looked "random" -a fairly common problem.

    • We take an input number such as 1,942.
    • Left pad it into a string: "0000001942"
    • Put the last two digits onto the front: "4200000019"
    • Convert that into a number: 4,200,000,019

    We now have a number that varies wildly between calls and is guaranteed to be less than 10,000,000,000. Not a bad start.

    • Convert that number to a Base 34 string: "2oevc0b"
    • Replace any zeros with 'y' and any ones with 'z': "2oevcyb"
    • Upshift: "2OEVCYB"

    The reason for choosing base 34 is so that we don't worry about 0/O and 1/l collisions. Now you have a short random-looking key that you can use to look up a LONG database identifier.

    0 讨论(0)
  • 2020-11-29 04:08

    Use UUID function.

    I don't know the source of your procedures in PHP that generates unique values. If it is library function they should guarantee that your value is really unique. Check in documentation. You should, hovewer, use this function all the time. If you, for example, use PHP function to generate unique value, and then you decide to use MySQL function, you can generate value that already exist. In this case putting UNIQUE INDEX on the column is also a good idea.

    0 讨论(0)
  • 2020-11-29 04:09

    Below is just for reference of numeric unique random id...

    it may help you...

    $query=mysql_query("select * from collectors_repair");
    $row=mysql_num_rows($query);
    $ind=0;
    if($row>0)
    {
    while($rowids=mysql_fetch_array($query))
    {
      $already_exists[$ind]=$rowids['collector_repair_reportid'];
    }
    }
    else
    {
      $already_exists[0]="nothing";
    }
        $break='false';
        while($break=='false'){
          $rand=mt_rand(10000,999999);
    
          if(array_search($rand,$alredy_exists)===false){
              $break='stop';
          }else{
    
          }
        }
    
     echo "random number is : ".$echo;
    

    and you can add char with the code like -> $rand=mt_rand(10000,999999) .$randomchar; // assume $radomchar contains char;

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