[PHP] random alphanumeric value generator

2004-01-26 Thread craig
Hi,

I am trying to generate a non-sequential random alphanumeric value. 
Just to make things fun, I need to guarantee that each value is unique.

I have a working solution, but it seems retarded to me because there
is a small chance that 2 people might hit the same page at the same 
time, do separate inserts, but both get the same MAX(uiIndex) value.
Is there a  better way to do this?

TIA,
Craig

table defn:
create table randomKey (
   uiIndex int unsigned auto_increment not null primary key,
   cKeychar(20) unique
);

function getKey() {
   //get random number into db. it is guaranteed 
   //unique from db once result = true because 
   //cKey is unique
   $query = 'INSERT INTO randomKey (cKey) VALUES (RAND())';
   while (!$result = mysql_query($query, dbConnect($errorMsg))) { 
  //do nothing, just loop until result = true
   }  

   //now get the random number, and MD5 it to get it alphanumeric
   $query = 'SELECT MAX(uiIndex), cKey FROM randomKey GROUP BY cKey';
   $result = mysql_query($query, dbConnect($errorMsg));
   $row = mysql_fetch_object($result);
   return md5($row-cKey);
}

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] random alphanumeric value generator

2004-01-26 Thread Matt Matijevich
because it is MySQL, couldn't you just use mysql_insert_id
http://www.php.net/manual/en/function.mysql-insert-id.php instead of
this:

   //now get the random number, and MD5 it to get it alphanumeric
   $query = 'SELECT MAX(uiIndex), cKey FROM randomKey GROUP BY cKey';
   $result = mysql_query($query, dbConnect($errorMsg));
   $row = mysql_fetch_object($result);

use this

  return md5(mysql_insert_id());

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] SOLVED: [PHP] random alphanumeric value generator

2004-01-26 Thread craig
I love this list!

That's perfect! I was able to simplify the function down even more.

Thanks a lot,
Craig

 -Original Message-
 From: Matt Matijevich [mailto:[EMAIL PROTECTED]
 Sent: January 26, 2004 4:01 PM
 To: [EMAIL PROTECTED]; [EMAIL PROTECTED]
 Subject: Re: [PHP] random alphanumeric value generator
 
 
 because it is MySQL, couldn't you just use mysql_insert_id
 http://www.php.net/manual/en/function.mysql-insert-id.php instead of
 this:
 
//now get the random number, and MD5 it to get it alphanumeric
$query = 'SELECT MAX(uiIndex), cKey FROM randomKey GROUP BY cKey';
$result = mysql_query($query, dbConnect($errorMsg));
$row = mysql_fetch_object($result);
 
 use this
 
   return md5(mysql_insert_id());
 
 
 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php