How do I generate unique transaction ID for payments or orders using PHP and MySQL preventing duplicacy even on simultaneous large number of requests?

I am trying to generate a unique code per transaction with prior consultation, in case it exists, it will automatically generate another without the need to re-execute the code.

Looking for references I was able to carry out the following code, but it only prints a piece of data, it prints the value 5, it is not generating a transaction code, for the moment I would like a total of 17 characters, numbers and letters in capital letters.

function randID($length) {
    $vowels = 'AEUY';
    $consonants = '0123456789BCDFGHJKLMNPQRSTVWXZ';
    $idnumber = '';
    $alt = time() % 2;
    for ($i = 0;$i < $length;$i++) {
        if ($alt == 1) {
            $idnumber.= $consonants[(rand() % strlen($consonants)) ];
            $alt = 0;
        } else {
            $idnumber.= $vowels[(rand() % strlen($vowels)) ];
            $alt = 1;
        }
    }

    return $length;
}

function genUniqueID($con) {
    $is_unique = 0;
    while ($is_unique === 0) {
        try {
            $randID = randID(5);
            $stmt = $con->prepare("SELECT COUNT(*) FROM tbl_payment_method WHERE id_transaction=?");
            $stmt->bind_param("s", $idnumber);
            $stmt->execute();
            $stmt->store_result();
            if ($stmt->num_rows>0) {
                $total_id_duplicated = null;
                $stmt->bind_result($total_id_duplicated);
                $stmt->fetch();
                $randID = randID(5);
            } else {
                $is_unique = 1;
            }
        } catch (Exception $e) {}
        return $randID;
    }
}

echo genUniqueID($con);

I have looked for several examples here, but all of them are only based on generating the code without querying the database, it is better to be sure that it is not going to be duplicated, then going back to my code, what am I doing wrong?

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum