Jump to content

Recommended Posts

Hi, im a newbe to php. I want to create a six digit random number as an ID but would like to also check if its not been already used within the database. Here is the code I have so far..
[code]
    $chars_for_actnum = array ("1","2","3","4","5","6","7","8","9","0");

        for ($i = 1; $i <= 6; $i++) {
            $actnum = $actnum . $chars_for_actnum[mt_rand (0, count ($chars_for_actnum)-1)];
            

            //make a loop so we can check the database if ID has already been taken. if it has we will genarate another ID.

$query = "Select * from ".$DBprefix."client_info where id='$actnum'";
$result = mysql_query($query);
if ($row = mysql_fetch_array($result)){
if  ($row["id"] == $actnum)        
}
    }[/code]

Any help will be much appreciated.
Thx

[b]EDITED BY WILDTEEN88: PLEASE USE THE CODE TAGS WHEN POSTING CODE, THANK YOU[/b]
Link to comment
https://forums.phpfreaks.com/topic/11978-do-while-loop/
Share on other sites

first for the random number can't you just do
[code]
$id = mt_rand (100000, 999999);
$query = mysql_query ("SELECT `id` FROM `table` WHERE `id` = '$id'") or die (mysql_error ());
if (mysql_num_rows ($query) != 0)
{
  $error = "blah";
}
else
{
  mysql_query ("INSERT...
}
[/code]
In any case are you looking for unique ids? you can always use an autoincremental index field in the database which automatically gives the id, and if it's for security or something just use something like
md5 (time().$_SERVER['REMOTE_ADDR']);
Link to comment
https://forums.phpfreaks.com/topic/11978-do-while-loop/#findComment-45507
Share on other sites

[code]
$query = "Select * from ".$DBprefix."client_info where id='$actnum'";
$result = mysql_query($query);
if ($row = mysql_fetch_array($result)){
if ($row["id"] == $actnum)
}
}
[/code]
Personally, and I haven't tried this, you should be able to do this:
[code]
$query = "Select * from ".$DBprefix."client_info where id='$actnum'";
$result = mysql_query($query);
$row = mysql_num_rows($result); //number of rows returned as matching.

if($row) {
   //carry out a repeat id generation perhaps, and then call function again?
} else {
   //do whatever you like if the number isn't taken, as in insert into database etc.
}
[/code]
$row = number of rows returned matching your query.
if(0) - or no matches, then expression if($row) will evaulate to false, as it's if(0) which is false!
if(1) - or if the number of matching rows is greater than 0, it will evaluate this expression to true.

HOwever, as mentioned above, if it's a unique id number, i would just go with making that field your primary key, and autoincrementing it. IE - you won't actually insert anything into that field, just insert the details instead and it will automatically fill in the field with a unique identifier upon insertion of the other fields information.
Link to comment
https://forums.phpfreaks.com/topic/11978-do-while-loop/#findComment-45508
Share on other sites

[code]

function GenCode() {
     $num =  rand(0, 9);

     for ($i = 1; $i <= 6; $i++) {
     $actnum = $actnum . $num;
     return $actnum;
}

//make a loop so we can check the database if ID has already been taken. if it has we will genarate another ID.

function ChkCode() {

     $actnum = GenCode();

     $query = "Select * from ".$DBprefix."client_info where id='$actnum'";
     $result = mysql_query($query);
    
     if (mysql_num_rows($result) <> 0) {
          return $actnum;
     } else {
          // The number already exists in the database try again.
          ChkCode();
}

$newCode = ChkCode; // This will return the new code already checked.
[/code]

You could also mod the script to add the new code into your database.
Link to comment
https://forums.phpfreaks.com/topic/11978-do-while-loop/#findComment-45512
Share on other sites

[!--quoteo(post=383815:date=Jun 14 2006, 03:27 PM:name=dharm11)--][div class=\'quotetop\']QUOTE(dharm11 @ Jun 14 2006, 03:27 PM) [snapback]383815[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Thanks for your help everyone.. autoincrementing is an option I considered but wouldn’t it be less efficient as I wouldn’t be able to replace expired members? By using a checked random number it should save disk space and ID numbers.
[/quote]

I did a Call Logging script once and i had the same problem as i had to reserve a job number before i submitted the job which ment i had loads of job ids that had no job attached.

I just created a script to clear all these out which run once a night but like you say the i could have a job with id230 and only have 12 jobs.

Could have done something similar to what you have done but that would mean that anyone looking through the past jobs may find job id 20 dated 2006 and job id 1 dated 2001 which is a confused mess waiting to happen. It just depens what your wanting to attach to your id's.
Link to comment
https://forums.phpfreaks.com/topic/11978-do-while-loop/#findComment-45561
Share on other sites

[!--quoteo(post=383829:date=Jun 14 2006, 03:40 PM:name=scripts2go.co.uk)--][div class=\'quotetop\']QUOTE(scripts2go.co.uk @ Jun 14 2006, 03:40 PM) [snapback]383829[/snapback][/div][div class=\'quotemain\'][!--quotec--]
I did a Call Logging script once and i had the same problem as i had to reserve a job number before i submitted the job which ment i had loads of job ids that had no job attached.

I just created a script to clear all these out which run once a night but like you say the i could have a job with id230 and only have 12 jobs.

Could have done something similar to what you have done but that would mean that anyone looking through the past jobs may find job id 20 dated 2006 and job id 1 dated 2001 which is a confused mess waiting to happen. It just depens what your wanting to attach to your id's.
[/quote]
I'd have to agree, I think you'd be much better off using an autoincremental value instead of random numbers, and just check for old users when adding the new one. I assume this is what you would be doing in any case with the random numbers as it would also need to check which are in use or not
Link to comment
https://forums.phpfreaks.com/topic/11978-do-while-loop/#findComment-45563
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.