Forum Moderators: coopster

Message Too Old, No Replies

Checking for duplicate number in mysql database

Number is randomly generated by PHP

         

dougmcc1

7:58 pm on Aug 24, 2003 (gmt 0)

10+ Year Member



I'm new at PHP and mysql. My script generates a random number and I want to make sure it's not already in the table before it get's added. If it is there, then I want to keep generating new random numbers until a unique one is generated and then add that unique number to the table.

Here is an example of what I want the code to do:


$rnum = mysql_query ("SELECT rnum FROM table;");
$random_number = rand(1,10000);
while ($random_number = $rnum) {
$random_number = rand(1,10000); //generate new number if it already exists in the table
}
/* code to add unique number to table */

This doesn't work because rnum = Resource id #3, so $random_number will never equal rnum (because rnum isn't even a number). Is there something I can do to modify this so it works or do I need to do something entirely different?

Thanks.

<added>rnum is a unique field by the way</added>

Dolemite

9:51 pm on Aug 24, 2003 (gmt 0)

10+ Year Member



If you don't expect to encounter duplicates very often, you should generate your random number, and then see if that specific number exists in your table. Use a while loop to keep generating & checking numbers.

Like this:

$row['rcnt']=1;

while($row['rcnt']>0)
{
$random_number = rand(1,10000);
$result = mysql_query('SELECT COUNT(rnum) as rcnt FROM table WHERE rnum="'.$random_number.'";');
$row = mysql_fetch_array($result);
}

There's probably a more clever way of doing this with just SQL using a unique key.

[edited by: jatar_k at 4:56 pm (utc) on Aug. 25, 2003]

dougmcc1

1:00 am on Aug 25, 2003 (gmt 0)

10+ Year Member




You should generate your random number, and then see if that specific number exists in your table. Use a while loop to keep generating & checking numbers

Take a look at the code in my first post. Is that not what I was doing in the first place? As I said, it was "just an example of what I want the code to do", but isn't it the same method as what you posted?

But thanks for your solution, I'll try it out some time this week.

[edited by: jatar_k at 4:57 pm (utc) on Aug. 25, 2003]

RobinC

1:26 am on Aug 25, 2003 (gmt 0)

10+ Year Member



I've got to put in my two pence worth - doing this sort of thing can get you into an infinate loop (depending on how good the random number generator is, and how many numbers you already have) - most of the time if I ever do anything like this, I make sure I've got a count in there, if it's still trying after 10/100/1000/etc goes then fail, and give a suitable message - it only ever happened to me once, and that was because I forgot to seed the random number generator (not php), but I'd prefer to have the "what-if" instead of the "oh s**t, why's CPU at 100%, and nobody getting any webpages?"

Just as an aside - if you know your random number generator code does xxx numbers without repeating, then why not use that, always seed with the same number, then keep an incriment counter, and get the n'th number, knowing it'll be unique?

dougmcc1

1:50 am on Aug 25, 2003 (gmt 0)

10+ Year Member



Hi Robin, thanks for your feedback. Can you elaborate on this please:

depending on how good the random number generator is, and how many numbers you already have

I'm just using rand() to generate the random numbers, nothing fancy. And I don't understand why it matters how many numbers are in the database. As long as it keeps generating numbers until that number isn't in the database, would that be sufficient?

I am randomly generating very large numbers and my database is small.... hmm I think I follow you now. If my database had 10 numbers (1-10) and my script only generated numbers between 1 and 10 I could see why there would be cause for concern. But I can't increment because I'm using these numbers in a way that the n'th number cannot be predetermined. My ID field auto increments but my rnum field needs to be completely random. BTW, what is seeding?

My loop times out after a few seconds so I don't have to worry about it being infinite.

Dolemite

3:57 am on Aug 25, 2003 (gmt 0)

10+ Year Member



Take a look at the code in my first post. Is that not what I was doing in the first place? As I said, it was "just an example of what I want the code to do", but isn't it the same method as what you posted?

No. It appears you were trying to retrieve all numbers from your database and then comparing those with your random number, except your weren't doing it right. My code compares a pair of numbers at a time.

[edited by: jatar_k at 4:58 pm (utc) on Aug. 25, 2003]

coopster

3:35 pm on Aug 25, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Have you considered using AUTO_INCREMENT on your rnum field in the database? Sounds like a perfect fit to me. Go to MySQL.com and search on AUTO_INCREMENT. This may work much better than the approach you are attempting.

dougmcc1

5:31 pm on Aug 25, 2003 (gmt 0)

10+ Year Member



Thanks coopster. Actually my rnum field needs to be completely random. But yeah AUTO_INCREMENT'ing isn't hard to apply, and that would be the ideal solution if I wasn't looking for randomization.

That does give me an idea though. I haven't tried Dolemites code yet but is there a way to AUTO_INCREMENT by a random number? If I could do that to my rnum field as a UNIQUE field, it should keep making random numbers until one is unique.

coopster

5:44 pm on Aug 25, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Sorry, but I think you've answered your own question here...
...is there a way to AUTO_INCREMENT by a random number?

AUTO_INCREMENT, by definition, is exactly what it says, auto-incrementation (no, not by random number). Think about it, if you auto-increment by random number, what could happen:

auto-increment + random number = new value
-------------- ------------- ---------
1 + 20 21
2 + 12 14
3 + 76 79
4 + 10 14
5 + 55 60

Oops, we get a duplicate. Sorry, no, it won't work.