1.11M Members

Auto Generate key

 
0
 
<?php 
function autogen()
{
     $qr="Select * from autogen where gid = 1";
     $we=mysql_query($qr)or die(mysql_error());
      while($brec=mysql_fetch_array($we))
      {
        $ln=$brec['autoln'];
      }            
      $lw= ++$ln;
      return $lw;
}
$newlead=autogen();
$qry="Select * from leads";
$r=mysql_query($qry) or die(mysql_error());
 while($re=mysql_fetch_array($r))
 {
    if($rec['leadno']==$newlead)
    {
        ++$newlead;
    }
 }
$er="UPDATE autogen SET autoln='$newlead'WHERE gid =1";
$bn=mysql_query($er)or die(mysql_error());
?>

This Code generates the key like LEA4545

but this code have some flaws same id could be inserted in table

although i have set in fuction in same found incre ent in it..

and the second flaw is this if i delete the keys ..then the new one created key must be from deleted keys

 
0
 

Let me repeat to see if I understood:

You want to generate unique keys yourself (IOW mysql autoincrement is not OK for your purpose). What is the format of the key and what are the rules for constructing the key?

then the new one created key must be from deleted keys

If this is the requirement then you have to keep the list of the deleted keys. If you keep it in a database table then just query for first available key.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article