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 8693037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:27:07+00:00 2026-06-13T00:27:07+00:00

I have a system that generates a batch of random codes up to 100

  • 0

I have a system that generates a batch of random codes up to 100 at a time. They are then inserted in to a database. I am using a code generator and it would be pretty unlikely a similar code could be created again but to make sure I want to check the DB for a duplicate anyway. My theory..

  $amount_of_codes


  while ($x != $amount_of_codes)
    {
    $code = gencode();
    $mysql_check = "SELECT code FROM data WHERE code = '$code'";
    $mysql_check_result = mysql_query($mysql_check);
    $check = mysql_num_rows($mysql_check_result);
     if (!$check)
      {
      mysql_query("INSERT INTO data(code) values('$code')")
      $x++
      } 
    }

seems messy, any better ideas?

  • 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-13T00:27:08+00:00Added an answer on June 13, 2026 at 12:27 am

    Give the Code Column a UNIQUE constraint:

    CREATE TABLE Code
    (
      Code ..
    ..
      CONSTRAINT Code_Unique UNIQUE (Code),
    ..
    );
    

    When inserting you only need to capture database exceptions/errors, not worrying if a Code already exists.

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

Sidebar

Related Questions

I have a module that generates fill out a System.Drawing.Graphics object. I then try
So I have a log in system that generates a random token for each
I have this piece of code that generates a random characters (ASCII) public char
I have a piece of code that generates the barcode image (using the Free
Currently, I have a system that actively generates the pages on my site using
Greetings! Situation: We have a CRM system that generates unique customer IDs. Now we
I'm developing a system, and I have build a code generator that emits a
We have a system that has some Bash scripts running besides Java code. Since
We have a system that runs in IIS. The system should always run using
I have a system that generates particles from sources and updates their positions. Currently,

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.