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

  • Home
  • SEARCH
  • 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 8485413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:38:16+00:00 2026-06-10T20:38:16+00:00

I wrote a function which makes a random id makeid(); Just to ensure the

  • 0

I wrote a function which makes a random id makeid(); Just to ensure the id is unique I have a SQL statement which checks if the id already exists.

$does_id_exist = mysql_query("SELECT COUNT(*) AS count FROM signups WHERE affid='$affid'");
if(mysql_num_rows($does_id_exist) == 1) 
  { 
  #loop function and perform query again
  }
else
  {
  #insert record
  }

So I’m having trouble with looping the function. How do I loop my function makeid() and perform the $does_id_exist check to ensure that each ID is unique.

–UPDATE– Just to clarify- My code makes an id like YES@281E But before I INSERT this id into the users record. I just need to verify IF any other user already has this id. IF another user has this id that event must trigger my function to create a new id e.g. WOW!29E3 and again check the sql/query to ensure no other user has that id. Continue to loop if fails or end and INSERT if the id is available.

  • 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-06-10T20:38:18+00:00Added an answer on June 10, 2026 at 8:38 pm

    You can either just use a primary key on your database table, or something like this:

    <?php
        // the id to insert
        $newId = null;
    
        // populate with results from a SELECT `aff_id` FROM `table`
        $currentIds = array(); 
    
        // prepopulate
        for( $i=0; $i<100000; $i++ )
        {
            $currentIds[] = "STRING_" + rand();
        }
    
        // generate at least one id
        do
        {
            $newId = "STRING_" + rand();
        }
        // while the id is taken (cached in $currentIds)
        while( in_array($newId, $currentIds) );
    
        // when we get here, we have an id that's not taken.
        echo $newId;    
    ?>
    

    Output:

    STRING_905649971 (run time 95ms);

    I’d definitely not recommend running the query repeatedly. Perhaps a final check before you insert, if your traffic volume is high enough.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function which makes an AJAX request to a server and returns
I have a function called: this.makeStuffHappen() Which makes different animations according to a switch
I wrote a function in Python which is used to tell me whether the
I wrote the function using js+jQuery. Here is a link to a jsFiddle which
I wrote a simple example, which estimates average time of calling virtual function, using
I have a javascript function I wrote to append a select box to a
I wrote this case select statement which refuses to work and I am unable
I wrote a program which solves some kind of optimization problem. I have been
I'm trying to make it so I can write a function which defines a
In C/C++, there is a 'write() function which let me write to either file

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.