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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:10:49+00:00 2026-05-27T14:10:49+00:00

I’m generating a random 10 character string with php, and inserting it into a

  • 0

I’m generating a random 10 character string with php, and inserting it into a column in my DB. My issue is that I want this string to be unique(in the database). I’ve thought of many ways of doing this, but wondering which way is the most efficient.

My PHP looks like this(random string can only be 10 chars long):

 //generates an almost unique(not quite) ticket number
 $pretrimmedtask = md5(uniqid(mt_rand(),true));
 $tasknum =  substr($pretrimmedtask ,0,10);

I then take this “unique” value and insert it. But because of the trim of the string, this value is by no means unique. I’m wondering what is the best way of making sure this value could never be duplicated, while still being efficient.

(I understand that querying the db to look for this value in there is possible… but I would rather do it in a more elegant fashion)

  • 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-27T14:10:50+00:00Added an answer on May 27, 2026 at 2:10 pm

    You should update your table and make the relevant column be a UNIQUE KEY, than try to insert the generated string, if no rows where inserted, generate another key and try again.

    ALTER TABLE table_name ADD UNIQUE KEY (column_name);
    

    The code below will try to INSERT a new row into table1, if unable it will try again with a different random generated $key.

    IE. the query will not succeed if col2 has a unique key constraint and the value of $key already exists in the column.

    function generate_random_string () {
      $charset = array_merge (
        range ('a', 'z'), range ('A','Z'), range ('0','0')
      );
    
      shuffle ($charset);
    
      return join ('', array_slice ($charset, 0, 9));
    }
    
    /* ....................................................... */
    
    do {
      $key = generate_random_string ();
    } while (
      !mysql_query ("INSERT INTO table1 (col1,col2) VALUES (123, '$key')")
    );
    

    You can of course use your own algorithm for generating random strings.

    NOTE: Make sure that the query can potentially succeed so that you don’t get caught in an endless loop.

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

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post

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.