Dynamo

6 replies to this thread. Most Recent

David Owen

22 Mar 2012, 9:16 pm

Create unique random record number in MySQL

What’s the best way to create a random number in MySQL to be used as the unique record number.

Has to be unique and random so it can’t be guessed as part of a sequence.

Is there an option within MySQL or using PHP?

David

quote

www.ineedwebhosting.co.uk

Freeway Friendly Web Hosting, Domain Names, VPS and Dedicated Servers. (Create an account it’s Free!)


PrintlineAdvertising.co.uk

Print Design, Digital and Litho Printers, Promotional Merchandise and Corporate Gifts.

waltd

22 Mar 2012, 9:24 pm

You want a UUID: http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html

Walter

On Mar 22, 2012, at 5:16 PM, David Owen wrote:

What’s the best way to create a random number in MySQL to be used as the unique record number.

Has to be unique and random so it can’t be guessed as part of a sequence.

Is there an option within MySQL or using PHP?

David

quote

Use Inlay - the deceptively simple CMS: http://inlay.io

Freeway user since 1997

http://www.walterdavisstudio.com

David Owen

22 Mar 2012, 10:03 pm

Thanks. It does not return as quite a user friendly number as I’d hoped. I’ll have to think of a different way.

David

On 22 Mar 2012, at 21:24, Walter Lee Davis <email@hidden> wrote:

You want a UUID

quote

www.ineedwebhosting.co.uk

Freeway Friendly Web Hosting, Domain Names, VPS and Dedicated Servers. (Create an account it’s Free!)


PrintlineAdvertising.co.uk

Print Design, Digital and Litho Printers, Promotional Merchandise and Corporate Gifts.

waltd

22 Mar 2012, 10:15 pm

What are you looking for? I have a little ruby method that can make a very nice alphanumeric code, or pure numeric, pretty much anything you like. It could be ported to anything, it’s a very neat method.

Walter

On Mar 22, 2012, at 6:03 PM, David Owen wrote:

Thanks. It does not return as quite a user friendly number as I’d hoped. I’ll have to think of a different way.

David

On 22 Mar 2012, at 21:24, Walter Lee Davis <email@hidden> wrote:

You want a UUID

quote

Use Inlay - the deceptively simple CMS: http://inlay.io

Freeway user since 1997

http://www.walterdavisstudio.com

David Owen

22 Mar 2012, 10:37 pm

I’m not entirely sure. Just some playing around with ideas which might come to use in a project in the pipeline.

Esentially the client needs to add a unique number at the end of an link in an email which would retrieve details on the site. I’m having a re-think as it just bound to go wrong on the client side. I’m thinking the only way would be a full registration login, etc. To do anything slightly clever.

David

On 22 Mar 2012, at 22:14, Walter Lee Davis <email@hidden> wrote:

What are you looking for?

quote

www.ineedwebhosting.co.uk

Freeway Friendly Web Hosting, Domain Names, VPS and Dedicated Servers. (Create an account it’s Free!)


PrintlineAdvertising.co.uk

Print Design, Digital and Litho Printers, Promotional Merchandise and Corporate Gifts.

waltd

22 Mar 2012, 11:27 pm

If the numbers have to be truly unique and random, then you need to store the ones you’ve used already so you don’t repeat yourself. So assume you’ll need a database. Then you make a random key, test to see if it exists already, if it does, make another one, if not, store it in the database and use it.

I’m not near the computer with that project on it, but I’ll send it later.

Walter

On Mar 22, 2012, at 6:37 PM, David Owen <email@hidden> wrote:

I’m not entirely sure. Just some playing around with ideas which might come to use in a project in the pipeline.

Esentially the client needs to add a unique number at the end of an link in an email which would retrieve details on the site. I’m having a re-think as it just bound to go wrong on the client side. I’m thinking the only way would be a full registration login, etc. To do anything slightly clever.

David

On 22 Mar 2012, at 22:14, Walter Lee Davis <email@hidden> wrote:

What are you looking for?

quote

Use Inlay - the deceptively simple CMS: http://inlay.io

Freeway user since 1997

http://www.walterdavisstudio.com

Back to Top

waltd

23 Mar 2012, 2:02 am

Here’s the Ruby version:

  def set_confirmation_code
    code = Array.new(7) { (0..9).to_a[rand(10)] }.join.to_s
    if Purchase.find_by_confirmation_code(code)
      return self.set_confirmation_code()
    else
      self.confirmation_code = code
    end
  end

To do this in PHP/MySQL, I would use MyActiveRecord and something like the following:

require('config.inc.php');
class Order extends MyActiveRecord {
}

function set_confirmation_code(){
    $code = '';
    foreach(range(0,7) as $digit){
        $code += (string) rand(0,9);
    }
    if(MyActiveRecord::FindFirst('Order', 'code = "' . $code . '"')){
        set_confirmation_code();
    }else{
        $order = MyActiveRecord::Create('Order', array('code' => $code));
        $order->save();
        return $code;
    }
}

Have a look at the Github project for MAR for more details about setting up a project with that framework. https://github.com/walterdavis/myactiverecord

Walter

On Mar 22, 2012, at 7:27 PM, Walter Lee Davis wrote:

If the numbers have to be truly unique and random, then you need to store the ones you’ve used already so you don’t repeat yourself. So assume you’ll need a database. Then you make a random key, test to see if it exists already, if it does, make another one, if not, store it in the database and use it.

I’m not near the computer with that project on it, but I’ll send it later.

Walter

On Mar 22, 2012, at 6:37 PM, David Owen <email@hidden> wrote:

I’m not entirely sure. Just some playing around with ideas which might come to use in a project in the pipeline.

Esentially the client needs to add a unique number at the end of an link in an email which would retrieve details on the site. I’m having a re-think as it just bound to go wrong on the client side. I’m thinking the only way would be a full registration login, etc. To do anything slightly clever.

David

On 22 Mar 2012, at 22:14, Walter Lee Davis <email@hidden> wrote:

What are you looking for?

quote

Use Inlay - the deceptively simple CMS: http://inlay.io

Freeway user since 1997

http://www.walterdavisstudio.com