不做位数要求时,可以用mysql字段的auto_increment 自动增加即可

解决方案 »

  1.   

    忘记了,不使用auto_increment 。
    位数可以不限制。
      

  2.   

    <?php
    mysql_connect('localhost', 'root', '');
    mysql_select_db('dbname');
    function GetRandNum($length){
    $min=1;
    $max=null;
    for($i=0;$i<$length;$i++){
    $max.=9;
    }
    srand((double)microtime()*1000000);
    $num=@rand($min, $max);
    return sprintf('%0'.$length.'d', $num);
    }
    $tablename='';
    $length=8;
    $idtemp=GetRandNum($length);
    while(!isset($repeat)){
    $query=mysql_query("select count(*) from ".$tablename." where id='".$idtemp."'");
    if(mysql_result($query, 0)>0) $idtemp=uniqid($idpre);
    else $repeat="uGain";
    }
    echo $idtemp;
    exit;
    ?>
      

  3.   

    guid -- 全局唯一标识符
    型如  54BB7788-0008-7789-ABAB-112233445566  这个样子的英文与数字的组合Guid Class
    License: GPLSample:$Guid = new Guid();
    print $Guid->toString();Binzy Wu
    [email protected]
    iZz Soft
    2004-07-08  20:26:16  Shanghai
    -----------------------------------------------下面是类的内容<?/* $Id: Guid.php,v 1.0 2004/07/08 05:50:17 binzy Exp $ */
    class System
    {
            function currentTimeMillis()
            {
                    list($usec, $sec) = explode(" ",microtime());
                    return $sec.substr($usec, 2, 3);
            }}class NetAddress
    {        var $Name = 'localhost';
            var $IP = '127.0.0.1';        function getLocalHost() // static
            {
                    $address = new NetAddress();
                    $address->Name = $_ENV["COMPUTERNAME"];
                    $address->IP = $_SERVER["SERVER_ADDR"];                return $address;
            }        function toString()
            {
                    return strtolower($this->Name.'/'.$this->IP);
            }}class Random
    {
            function nextLong()
            {
                    $tmp = rand(0,1)?'-':'';
                    return $tmp.rand(1000, 9999).rand(1000, 9999).rand(1000, 9999).rand(100, 999).rand(100, 999);
            }
    }// 三段
    // 一段是微秒 一段是地址 一段是随机数
    class Guid
    {        var $valueBeforeMD5;
            var $valueAfterMD5;        function Guid()
            {
                    $this->getGuid();
            }
    //
            function getGuid()
            {
                    $address = NetAddress::getLocalHost();
                    $this->valueBeforeMD5 = $address->toString().':'.System::currentTimeMillis().':'.Random::nextLong();
                    $this->valueAfterMD5 = md5($this->valueBeforeMD5);
            }        function newGuid()
            {
                    $Guid = new Guid();
                    return $Guid;
            }        function toString()
            {
                    $raw = strtoupper($this->valueAfterMD5);
                    return substr($raw,0,8).'-'.substr($raw,8,4).'-'.substr($raw,12,4).'-'.substr($raw,16,4).'-'.substr($raw,20);
            }}?>
      

  4.   

    $repeat、$idpre 分别是指什么?
      

  5.   

    <?php
    mysql_connect('localhost', 'root', '');
    mysql_select_db('dbname');
    function GetRandNum($length){
    $min=1;
    $max=null;
    for($i=0;$i<$length;$i++){
    $max.=9;
    }
    srand((double)microtime()*1000000);
    $num=@rand($min, $max);
    return sprintf('%0'.$length.'d', $num);
    }
    $tablename='';
    $length=8;
    $idtemp=GetRandNum($length);
    while(!isset($repeat)){
    $query=mysql_query("select count(*) from ".$tablename." where id='".$idtemp."'");
    if(mysql_result($query, 0)>0) $idtemp=GetRandNum($length);
    else $repeat="uGain";
    }
    echo $idtemp;
    exit;
    ?>
    更正一下
    $repeat是重复标志,如果数据库里有这个id,$repeat无值,while继续执行,直接生成的随机数与数据库里的记录不重复,给$repeat赋值,循环结束.
    刚才的$idtemp=uniqid($idpre);是另一个程序中,用uniqid()取唯一字串做id用的.
    uniqie($idpre)是用来得到一个以$idpre开头加13位随机字串的字串.
      

  6.   

    TO   uGain(uGain.org)如果可以一次性生成唯一数字最好的!
    TO   erldy(我还是我)
    你这样的做法,遇到局域网的多用户同时注册,通的过吗?
      

  7.   

    看看pear/upload.php中的对文件的改名八
      

  8.   

    我的是PHP4,怎么没这个文件啊?
      

  9.   

    echo preg_replace('/\d\.(\d+)\s(\d+)/', '\\2\\1', microtime());
    直接用当前的带微秒的时间戳
      

  10.   

    时间+随机传+Apache当前pid+用户名MD5
    绝对不会有重复的:D
      

  11.   

    晕,将auot_increament取出来再用md5加密一下给用户做账号好啦