0

I have a service on the internet where people post pictures and a short string is generated. Only one can be used ever. However, I am getting into duplicates in the database and I am seeing major problems.

Here's what I am using:

$id=rand(10000,99999);
$short_string = base_convert($id,20,36);

What would be the best way to fix it? Check from the database and keep looping till it doesn't match one? What if every possible solution and it goes in an infinite loop?

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
iosfreak
  • 5,228
  • 11
  • 59
  • 102
  • by the code you posted, you can only have 89999 numeric string ids. Not very many. Are these permanently in use? or do you remember and recycle them? – Mitch Wheat May 21 '11 at 02:24
  • 1
    question: base_convert( string $number, int $frombase, int $tobase ). I may be missing something but isn't your frombase, base 10 and not 20? – Mitch Wheat May 21 '11 at 02:42
  • @Mitch Wheat: That's the code I use, exactly. What does changing the base from 10 to 20 do? – iosfreak May 21 '11 at 02:56

5 Answers5

1

Put your PK through an algorithm that generates a unique number from it and put that through your function.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
1

The best bet would be to make sure the image doesn't exist by using the random number generator against the list of images before writing a new image with that number in it.

Try to increase the amount and type of characters by using an algorithm that uses numbers, letters so it increases the combinations that you can have.

BRampersad
  • 862
  • 1
  • 12
  • 25
  • But doesn't that still put it at risk for getting the same result more than once? – iosfreak May 21 '11 at 01:49
  • You can count in order of increasing numbers instead of random numbers if you truely want to never have repeating numbers but if you choose to go the random way there will always be a chance of having dups. – BRampersad May 21 '11 at 02:34
  • That would probably introduce a race condition if two processes were racing to insert the same value; both checks could succeed. – MarkR May 21 '11 at 06:22
1

Increment the last value by a random amount instead of using a random value. Like so:

$results = mysql_query("SELECT * FROM thetable ORDER BY theId DESC LIMIT 1");
$keyToUse = 1;
if($row = mysql_fetch_array($results)) {
    $keyToUse = (int)$row['theId'] + rand(1, 100);
}

Then convert the integer key to and from any format, say using base_convert.

Ry-
  • 218,210
  • 55
  • 464
  • 476
  • This is exactally what I did. Except I added 10 random numbers to it. I don't think it will get repeated any time soon. – iosfreak May 21 '11 at 03:03
1

Did you try using mt_rand() http://www.php.net/manual/en/function.mt-rand.php. Also you should be increasing the range.

Shameer
  • 3,036
  • 1
  • 21
  • 27
0

Use md5 http://php.net/manual/en/function.md5.php The strings produced have no conflict (with large probability).

George Kastrinis
  • 4,924
  • 4
  • 29
  • 46