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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:33:08+00:00 2026-06-09T09:33:08+00:00

How can i check if a id exists in a mysql database, and if

  • 0

How can i check if a id exists in a mysql database, and if it does: create a new id and check over again. I have the following code to create a id:

$length = 10;
$id = '';
for ($i = 0; $i < $length; $i++) {
    $id .= chr(rand(ord('0'), ord('9')));
}

To check if the id exists i use the code below:

$database->query('SELECT user_id FROM users 
                  WHERE public_id = ':public_id',array(':public_id' => $public_id)');

if($database->count() >= '1') { 
//create a new id 
} else { } 
  • 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-09T09:33:10+00:00Added an answer on June 9, 2026 at 9:33 am

    What you’re trying to do is not considered a “best practice”, as (theoretically) you could create an infinite loop (creating a random ID, then checking for existence could “hit” on each iteration).

    A better solution to create unique IDs is to set a column as your primary key, and increment upon each new insert.

    If you want another column to be unique, you can define that column to be unique as well, but you won’t have to rely on “random” strings.

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

Sidebar

Related Questions

can I check if a file exists at a URL? This link is very
How can I check if a key exists in the sub keys of an
How can I check if an email folder exists using Zend_Mail_Storage_Imap, theres a createFOlder,
How can i check whether a package like javax.servlet.* exists or not in my
How can I check if the text I'm looking for exists in this html
I can check for iPhone with this code: (navigator.userAgent.match(/iPhone/i)) But I want to target
With this code I can check the caret position in a textarea in firefox:
I am trying to set up a MySQL trigger that does the following: When
I have a mySQL database and I have a Perl script that connects to
I got the following trigger on my sql server 2008 database CREATE TRIGGER tr_check_stoelen

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.