Answer the question
In order to leave comments, you need to log in
How to correctly choose a unique value in relation to an array unloaded from the database?
When you add an element to the database, you must assign a unique key to it.
Numeric is not suitable, since you can parse all products with a simple search.
How to make a cycle of checking for uniqueness?
Which will generate a key until there are no duplicates.
uniqid();
generates suitable keys, but does not guarantee the uniqueness of the value, so I decided to check with the available keys.
Unloading a column with keys from the database
$arrayAllKey = R::getAll('SELECT `category_key` FROM `items` WHERE `id_user` = 104);
// Указываю существующий ключ, чтобы цикл нашел дубль
$unique = "5e9dfcc637f75";
// Берем значение сгенерированного ключа $unique
// Если найдет значение, то выдаст его, если нет, ничего не выдаст
$findedKey = R::getAll('SELECT `category_key` FROM `items` WHERE `id_user` = ? and `category_key` = ?', [104, $unique]);
// Если значение найденного ключа есть
if ($findedKey[0]["category_key"]) {
// делаем генерацию нового ключа
$unique = uniqid();
// снова выбираем значение из бд
$findedKey = R::getAll('SELECT `category_key` FROM `items` WHERE `id_user` = ? and `category_key` = ?', [104, $unique]);
} else {
// если дублей не найдено, выводим уникальный ключ
echo $unique;
};
Answer the question
In order to leave comments, you need to log in
UUID v4 will save you.
PS But if they want to spar, they will spar. They will simply take a list of products directly from your page. You're going to publish them all somewhere.
Your goods, if at least someone needs them for a hundred years at lunch, will be paired with numerical, alphabetic, alphanumeric, unique, unrepeatable, imaginary, hidden, secret, encrypted, hidden in any way indexes.
So do not torture your fragile brain, make digital indexes.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question