Search Posts

Author: xutiu

Efficient check if value exists in database

I want to generate unique value and save it in database. function generateRandomString($length = 10) { $characters = ‘0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ’; $charactersLength = strlen($characters); $randomString = ”; for ($i = 0; $i < $length; $i++) { $randomString .= $characters[rand(0, $charactersLength – 1)]; } return $randomString; } I have two options (PSEUDO CODE): 1) $allValues = ‘SELECT unique_number FROM table’; //EXAMPLE! $number = generateRandomString(); while(in_array(number, $allValues)) { $number = generateRandomString(); } 2) function getUniqueNumber($uniqueNumber) { return ‘SELECT unique_number […]