Gossamer Forum
Home : General : Perl Programming :

Perl/MySQL Unique ID

Quote Reply
Perl/MySQL Unique ID
Hi

I've got the following, untested code. I want to know if this will work, and if there is a better way of doing what I'm trying to do? I'm afraid this might trigger itself into endless loops? Wouldn't it be better to split this into two seperate subs?

The sub is suppose to create a random ID, check the ID colum of a MySQL table to see if the ID already exists. If it doesn't it returns normally, if it does it should go through the sub again until a random ID is found.

Code:
sub get_unique_id {

$randlet = chr(65 + int(rand(26))) . chr(65 + int(rand(26)));
$randnum = 1000000 + int(rand(8999999));
$randid = $randlet . $randnum;

&connect_to_db;

$sth = $dbh->prepare("SELECT id FROM bs_sessions");
$sth->execute();

my $count = 0;
while (my $ref = $sth->fetchrow_hashref ())
{
if ($randid eq $ref->{id}) {
$count + 1;
}
}

$sth->finish();

if ($count) {
&get_unique_id;
exit;
}

return $randid;
}

Rgds
Wil Stephens

- wil

Last edited by:

Wil: Nov 15, 2001, 7:57 AM
Quote Reply
Re: [Wil] Perl/MySQL Unique ID In reply to
The potential for recursion is there although it doesnt seem too likely. I think something like SELECT * FROM bs_sessions WHERE id = $randid would eliminate the need to loop through all the results.

Regards,
Charlie
Quote Reply
Re: [Wil] Perl/MySQL Unique ID In reply to
Code:
sub get_unique_id {

$randlet = chr(65 + int(rand(26))) . chr(65 + int(rand(26)));
$randnum = 1000000 + int(rand(8999999));
$randid = $randlet . $randnum;

&connect_to_db;

$sth = $dbh->prepare("SELECT id FROM bs_sessions WHERE id='$randid'");
$sth->execute();

my $num = $sth->fetchrow_hashref;

$sth->finish();

$num->{id} ? &get_unique_id : return $randid;

}

Pretty similar still. You probably won't get stuck in a loop unless your table fills up.
Quote Reply
Re: [PaulW] Perl/MySQL Unique ID In reply to
Thanks for that. I overlooked that completly! So, this should work OK then?

Code:
sub get_unique_id {

$randlet = chr(65 + int(rand(26))) . chr(65 + int(rand(26)));
$randnum = 1000000 + int(rand(8999999));
$randid = $randlet . $randnum;

&connect_to_db;

$sth = $dbh->prepare("SELECT id FROM bs_sessions WHERE id = $randid");
$sth->execute();

my $isnumrand = $sth->fetchrow_hashref;

$sth->finish();

if ($isnumrand) {
&get_unique_id;
exit;
}

return $randid;
}

- wil
Quote Reply
Re: [Wil] Perl/MySQL Unique ID In reply to
No that won't work. $isnumrand is a hashref so you can't do..

if ($isnumrand) {

Use the way I provided (if you want).

Last edited by:

PaulW: Nov 15, 2001, 8:49 AM
Quote Reply
Re: [PaulW] Perl/MySQL Unique ID In reply to
But the hashref is not true if the ID doesn't exist?

The line:

$num->{id} ? &get_unique_id : return $randid;

which you provided doesn't make sens to me. Can you elaborate?

Cheers

- wil
Quote Reply
Re: [Wil] Perl/MySQL Unique ID In reply to
$num->{id} ? &get_unique_id : return $randid;

In basic terms...

If id is already in database, generate a new id otherwise return.
Quote Reply
Re: [PaulW] Perl/MySQL Unique ID In reply to
So you're saying that line of code wroks, or do I need to make a loop ouf of that you are saying?

Bare with me... it's been a long day.

- wil
Quote Reply
Re: [Wil] Perl/MySQL Unique ID In reply to
No.. what you're saying won't work. If you've closed the database handle then you can't reference back to it.

You'll need to keep the database handle open, and use a while loop for what you're trying to do?


- wil
Quote Reply
Re: [Wil] Perl/MySQL Unique ID In reply to
No you don't at all. You are closing the handle but then you are re-executing the subroutine re-creating the handle.

Last edited by:

PaulW: Nov 15, 2001, 2:15 PM
Quote Reply
Re: [PaulW] Perl/MySQL Unique ID In reply to
Sorry, I just can't get my head around that line of code at all.

Can you annotate it for me?

- wil
Quote Reply
Re: [Wil] Perl/MySQL Unique ID In reply to
I thought I already gave a basic explanation. Do I need to go down to teletubbie level? j/k Wink


$num->{id} ? &get_unique_id : return $randid;

$num->{id} will contain the id number selected from the database if it exists otherwise it will be empty.

$num->{id} ? is like a question hence the ?

Does $num->{id} exist?

If it does then we need to generate a new id so we loop back to the beginning of the sub and do it all over again. Otherwise we can return to the caller



Last edited by:

PaulW: Nov 15, 2001, 2:27 PM
Quote Reply
Re: [PaulW] Perl/MySQL Unique ID In reply to
Waw. I never knew you could write it so simple, and that's where I got all confused.

Thanks for hanging in there with me. Much appreciated.

I would of written what you said as:

Code:
if ($num->{id}) {
&get_unique_id;
exit; <- do I need this exit call ???
}


return $randid;

I'm still concerned about $num->{id} though. Wouldn't I have to say,

while sth->fetchrow_hashref {

to get $num->{id} ?


- wil
Quote Reply
Re: [Wil] Perl/MySQL Unique ID In reply to
No the exit is not needed as you will never get to that point. It either re-calls the sub-routine or skips the if block altogether and returns.

No you don't need a while loop.

my $num = $sth->fetchrow_hashref; is fine.
Quote Reply
Re: [PaulW] Perl/MySQL Unique ID In reply to
So why can't I used if ($num).

Sorry to be a pain. I really don't get that point.

Ah, I've got the solution -> bed, sleep. zzzzzz <g>.

- wil
Quote Reply
Re: [Wil] Perl/MySQL Unique ID In reply to
Because $num will always be a hash reference. It's value will be something like HASH(0xfc3c4) so

if ($num) {

will always be true (I think)

Last edited by:

PaulW: Nov 15, 2001, 3:00 PM
Quote Reply
Re: [PaulW] Perl/MySQL Unique ID In reply to
Hmm. Still don't understand how you can get $num->{id}.

I guess I'll have to try out the various lines of code on my box tomorrow. No point trying anything out on this old 133 at home.

I hate reading code and not being able to execute it <g>.

Cheers

Wil

- wil
Quote Reply
Re: [Wil] Perl/MySQL Unique ID In reply to
>>Still don't understand how you can get $num->{id}.
<<

What do you mean?

You are selecting the ID from _one_ row, therefore:

my $num = $sth->fetchrow_hashref;

...selects the ID and as it is a hash reference you need $num->{id}. It is similar to:

$num = { id => 'value of ID' };


Last edited by:

PaulW: Nov 15, 2001, 3:17 PM
Quote Reply
Re: [PaulW] Perl/MySQL Unique ID In reply to
Now reading that hashref line makes a lot more sense.

Sorry, I really need to log off. My brain is completly not functioning anymore.

15 hours in front of trhis monitor is not good. <g>.

- wil
Quote Reply
Re: [Wil] Perl/MySQL Unique ID In reply to
Code:
sub get_unique_id {

$randlet = chr(65 + int(rand(26))) . chr(65 + int(rand(26)));
$randnum = 100_000 + int rand 900_000;
$randid = $randlet . $randnum;

&connect_to_db;

$sth = $dbh->prepare("SELECT id FROM bs_sessions WHERE id = $randid");
$sth->execute();

my $isnumrand = $sth->fetchrow_hashref;

$sth->finish();

if ($isnumrand->{id}) {
&get_unique_id;
}

return $randid;
}

Should work OK? I'm assuming that if there is no such ID in the database then $isnumrand->{id} would be 0 or N/A?

Wil

- wil
Quote Reply
Re: [Wil] Perl/MySQL Unique ID In reply to
Yeah but why do you keep going back to the if {} block.
Quote Reply
Re: [PaulW] Perl/MySQL Unique ID In reply to
Personal preference, I guess. I find it easier to read on the page.

Thanks

- wil
Quote Reply
Re: [Wil] Perl/MySQL Unique ID In reply to
Using the other method is quicker (in terms of execution)
Quote Reply
Re: [PaulW] Perl/MySQL Unique ID In reply to
Yes, you're probably right. I'm just trying to get my head around writing my first big perl script for ages and it's killing me :-(

- wil