I want to check if a key exists in a MySQL DB. I can do this ok but I would need to create a new key if it does exist.

So this is the function that makes the key:

function keygen($a){
	$b = str_shuffle('abcdefghijklmnopqrstuvwxyz1234567890');
	return substr($b,0,$a);
}

So I thought about making a while loop to check if the key exists. But I don't know how to form it.

Could someone explain please. Thanks.

I want to check if a key exists in a MySQL DB. I can do this ok but I would need to create a new key if it does exist.

So this is the function that makes the key:

function keygen($a){
	$b = str_shuffle('abcdefghijklmnopqrstuvwxyz1234567890');
	return substr($b,0,$a);
}

So I thought about making a while loop to check if the key exists. But I don't know how to form it.

Could someone explain please. Thanks.

this is my suggestion only if you agree or not.

create one field on your table and auto-number it.
create a dd-mm-yyyy + 0000000000 + autonum
1004200900000001
1004200900000002
1004200900000003
all are unique so no need to worry about...

Well I wanted the key to be from the letters in $b.

Well I wanted the key to be from the letters in $b.

function xconfirmUserID($b) {
      /* Add slashes if necessary (for query) */
      if(!get_magic_quotes_gpc()) {
	      $b = addslashes($b);
      }

      /* Verify that ID is in database */
      $q = "SELECT trans_mid FROM ".TBL_USERS." WHERE trans_mid = '$b'";
      $result = mysql_query($q, $this->connection);
      if(!$result || (mysql_numrows($result) < 1)){
         return 1; //Indicates ID failure
      }

      /* Retrieve ID from result, strip slashes */
      $dbarray = mysql_fetch_array($result);
      $dbarray['trans_mid'] = stripslashes($dbarray['trans_mid']);

      /* Validate that password is correct */
      if($b == $dbarray['trans_mid']){
         return 0; //Success! ID confirmed
      }
      else{
         return 2; //Indicates ID failure
      }
   }

Get the return value.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.