Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3614736
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:17:13+00:00 2026-05-18T22:17:13+00:00

This is a follow-up question to a question I posted here . I am

  • 0

This is a follow-up question to a question I posted here.

I am using the following code to give users a unique id:

function NewGuid() { 
    $s = strtoupper(uniqid(rand(),true)); 
    $guidText = substr($s,0,8) . '-' . substr($s,8,4) . '-' . substr($s,12,4). '-' . substr($s,16,4). '-' . substr($s,20); return $guidText; 
} 

$Guid = NewGuid();
echo $Guid;

$alphabet = '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ';

function base_encode($num, $alphabet) { 
    $base_count = strlen($alphabet); 
    $encoded = '';

    while ($num >= $base_count) {
        $div = $num/$base_count;
        $mod = ($num-($base_count*intval($div)));
        $encoded = $alphabet[$mod] . $encoded;
        $num = intval($div);
    }

    if ($num) $encoded = $alphabet[$num] . $encoded;
    return $encoded;    
}

function base_decode($num, $alphabet) { 
    $decoded = 0;
    $multi = 1;

    while (strlen($num) > 0) {
        $digit = $num[strlen($num)-1];
        $decoded += $multi * strpos($alphabet, $digit);
        $multi = $multi * strlen($alphabet);
        $num = substr($num, 0, -1);
    }

    return $decoded;
}

echo base_encode($Guid, $alphabet);

So, if a id that is generated is already in use, i want this code to be able to repeat until a completely new id is generated and entered into the database. IN addition i am new to programming, so please feel free to elaborate 😉

Any thoughts? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T22:17:13+00:00Added an answer on May 18, 2026 at 10:17 pm

    Super easy.

    You have 3 functions.

    One – main generate guid function.
    Two – Sub generate guid function.
    Three – Sub insert guid function.

    Sub generate function:
    Does the actual generation, returns a guid.

    Sub insert function:
    Attempts to insert guid into the database. Returns false/null on failure. True/data on success.

    Main function:
    In a loop:
    Call subfunction to get a guid.
    Call subfunction to insert in the database where the guid is a unique or primary key.
    On failure, restart the loop.
    Success exits the loop and returns the guid.

    Example:

    function GetGuid()
    {
        do
        {
            $guid = GenerateGuid();
        } while (!InsertGuid($guid));
        return $guid;
    }
    
    function GenerateGuid()
    {
        // do your stuff here.
        return $guid;
    }
    
    function InsertGuid($guid)
    {
        $sql = 'INSERT INTO `guid_table` (`guid`) VALUES ('$guid')';
        $connection = //do sql connect here;
        return // do sql execution here which will return false/null on failure;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This post is a follow-up of a related question posted here by Ran .
This is a follow-up question to the one I posted here (thanks to mario
This is a follow-up to an earlier question I posted on EF4 entity keys
This is a follow up on the question Android accesing soap service posted by
This is a follow-on question from the one I asked here . Can constraints
This is a follow-up question to the following if you would like to see:
This is somewhat of a follow up to a question posted earlier last month.
This is a follow-up to a question I posted a while back: Can I
This question follows on from the problem posted here when i run explain I
this is a follow-up to the question I posted yesterday. I was able to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.