Check Key Doesn't Exist

Discussion in 'PHP' started by !Unreal, Apr 9, 2009.

  1. #1
    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);
    }
    
    PHP:
    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.
     
    !Unreal, Apr 9, 2009 IP
  2. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #2
    There are hashing algorithms for this purpose already, like md5() and sha1().

    $id = mysql_insert_id();
    $hash = md5("my_salt_{$id}");
    $sql = "update row set my_key = '{$hash}'";
    $st = mysql_query($sql) or die(mysql_error());
    Code (markup):
     
    SmallPotatoes, Apr 9, 2009 IP
  3. matthewrobertbell

    matthewrobertbell Peon

    Messages:
    781
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #3
    run the query select from table where key="yourkey"

    then use mysql_num_rows to see how many rows contain that key (should be 1 or 0 for you)
     
    matthewrobertbell, Apr 10, 2009 IP