My custom Short URL service, running out of IDs?

I have made a custom shorturl type service and for whatever reasons I keep running out of unique ID’s. I have the ID set as primary key / unique records only. When I generate for example 1,000 links, it might come back with only 400 or so instead of exactly 1,000. How does a service like generate enough ID’s without having the increase the number of characters in the link ID?

Am I doing something wrong? Should my code keep looping until the number of ID’s/links is generated? Thanks in advance.


$code = createRandomPassword(); 

$sql = "INSERT INTO id (code, enabled, ip, site) VALUES ('$code', '1', '', '$site')" or die(mysql_error());


if (mysql_query($sql)) {
echo $customsite . "/?id=" . $code . "\


  function createRandomPassword() { 

    $chars = "abcdefghijkmnopqrstuvwxyz023456789"; 
    $i = 0; 
    $pass = '' ; 

    while ($i <= 13) { 
        $num = rand() % 34; 
        $tmp = substr($chars, $num, 1); 
        $pass = $pass . $tmp; 

    return $pass; 

Oooo… I’ll look into that. That sounds like it will solve my last issue!

Check out these posts from me, it explains how I addressed this. :slight_smile:

It seems all I had to do was move the i variable incrementing to the if successful function! But… if and when I actually do run out of unique ID’s… it will loop and god knows what will happen…

Any ideas?