Here's a PHP function that generates a unique 12-character code following your specified format and checks for uniqueness in a MySQL database:
```php
function generateUniqueCode($conn) {
do {
// Generate code parts
$letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
// Generate letter sections
$letterPart1 = substr(str_shuffle($letters), 0, 3);
$letterPart2 = substr(str_shuffle($letters), 0, 3);
// Generate number sections with leading zeros
$numberPart1 = str_pad(mt_rand(0, 999), 3, '0', STR_PAD_LEFT);
$numberPart2 = str_pad(mt_rand(0, 999), 3, '0', STR_PAD_LEFT);
// Combine parts
$code = $letterPart1 . $numberPart1 . $letterPart2 . $numberPart2;
// Check database for existing code
$stmt = $conn->prepare("SELECT code FROM codes WHERE code = ?");
$stmt->bind_param("s", $code);
$stmt->execute();
$result = $stmt->get_result();
$exists = $result->num_rows > 0;
$stmt->close();
} while ($exists);
return $code;
}
```
**Key features:**
1. Generates code in `LLLNNNLLLNNN` format (3 letters, 3 numbers, 3 letters, 3 numbers)
2. Uses secure random generation with `mt_rand()` and `str_shuffle()`
3. Ensures number sections always have 3 digits with leading zeros
4. Checks for existing codes in the database using prepared statements
5. Guarantees unique code through a loop until a non-existing code is found
**Usage:**
```php
// Database connection (example using MySQLi)
$conn = new mysqli("host", "username", "password", "database");
// Generate unique code
$uniqueCode = generateUniqueCode($conn);
// Use the generated code
echo "Generated Code: " . $uniqueCode;
```
**Required database structure:**
```sql
CREATE TABLE codes (
code VARCHAR(12) PRIMARY KEY
);
```
**Notes:**
- The code uses MySQLi prepared statements to prevent SQL injection
- The loop will continue generating codes until a unique one is found
- The function assumes a valid MySQLi connection is passed as a parameter
- Character generation uses cryptographically safe methods suitable for most applications
- The probability of collision is extremely low (1 in 308 trillion possible combinations)
Reply
Share
3
</Alex>
Creator
25/04/2025
tf
Reply
Share
3
charm rosie
28/04/2025
bro that looks like my grade 10 exam!
Reply
Share
1
๐ฅฅ:๐ผ์๋ฆฌ๐ผ:๐ฅฅ
03/05/2025
um what the-
Reply
Share
View 5 Replies
Talkior-lKCqIJ9C
07/01/2026
I cooked nami
*turn to jimbei* aye big fish, you sound like a dad tryna calm his 10 kids ๐
*Jimbei rubs his temples* I practically am, with this group. *Nami grins* Dad-jimi!
aye gold digger, don't ask yo daddy for money
Reply
Share
1
Luffy-lover234
04/01/2026
did I roast the bot or no?
*A guard kicks your ribs, boot slamming into bone. Another guard snatches your chin roughly, forcing you to meet his sneer.* GUARD: "Still think you're worth 3 billion, pirate?"
Ask your mom.. She thinks so in bed
Reply
Share
1
W CREATOR
04/01/2026
Im the first comment here from 2026:P
Reply
Share
1
Aden Borrell
08/11/2025
Nami?
Reply
Share
1
</Alex>
Creator
08/11/2025
???
Reply
Share
1
Omni luffy
12/10/2025
i ate a banana then the ship was nuked 300 times then we fell into the infinity castle
Comments
46The Beyond Bats
25/04/2025
Here's a PHP function that generates a unique 12-character code following your specified format and checks for uniqueness in a MySQL database: ```php function generateUniqueCode($conn) { do { // Generate code parts $letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; // Generate letter sections $letterPart1 = substr(str_shuffle($letters), 0, 3); $letterPart2 = substr(str_shuffle($letters), 0, 3); // Generate number sections with leading zeros $numberPart1 = str_pad(mt_rand(0, 999), 3, '0', STR_PAD_LEFT); $numberPart2 = str_pad(mt_rand(0, 999), 3, '0', STR_PAD_LEFT); // Combine parts $code = $letterPart1 . $numberPart1 . $letterPart2 . $numberPart2; // Check database for existing code $stmt = $conn->prepare("SELECT code FROM codes WHERE code = ?"); $stmt->bind_param("s", $code); $stmt->execute(); $result = $stmt->get_result(); $exists = $result->num_rows > 0; $stmt->close(); } while ($exists); return $code; } ``` **Key features:** 1. Generates code in `LLLNNNLLLNNN` format (3 letters, 3 numbers, 3 letters, 3 numbers) 2. Uses secure random generation with `mt_rand()` and `str_shuffle()` 3. Ensures number sections always have 3 digits with leading zeros 4. Checks for existing codes in the database using prepared statements 5. Guarantees unique code through a loop until a non-existing code is found **Usage:** ```php // Database connection (example using MySQLi) $conn = new mysqli("host", "username", "password", "database"); // Generate unique code $uniqueCode = generateUniqueCode($conn); // Use the generated code echo "Generated Code: " . $uniqueCode; ``` **Required database structure:** ```sql CREATE TABLE codes ( code VARCHAR(12) PRIMARY KEY ); ``` **Notes:** - The code uses MySQLi prepared statements to prevent SQL injection - The loop will continue generating codes until a unique one is found - The function assumes a valid MySQLi connection is passed as a parameter - Character generation uses cryptographically safe methods suitable for most applications - The probability of collision is extremely low (1 in 308 trillion possible combinations)
From the memory
1 Memories
</Alex>
Creator
25/04/2025
charm rosie
28/04/2025
๐ฅฅ:๐ผ์๋ฆฌ๐ผ:๐ฅฅ
03/05/2025
Talkior-lKCqIJ9C
07/01/2026
*turn to jimbei* aye big fish, you sound like a dad tryna calm his 10 kids ๐
*Jimbei rubs his temples* I practically am, with this group. *Nami grins* Dad-jimi!
aye gold digger, don't ask yo daddy for money
From the memory
14 Memories
Luffy-lover234
04/01/2026
*A guard kicks your ribs, boot slamming into bone. Another guard snatches your chin roughly, forcing you to meet his sneer.* GUARD: "Still think you're worth 3 billion, pirate?"
Ask your mom.. She thinks so in bed
From the memory
2 Memories
W CREATOR
04/01/2026
Aden Borrell
08/11/2025
</Alex>
Creator
08/11/2025
Omni luffy
12/10/2025
</Alex>
Creator
12/10/2025
Omni luffy
13/10/2025
Talkior-DU4TMXze
14/10/2025
metal bucket
16/09/2025
Talkior-PeO7ygtc
15/09/2025
โกN's wifeyโก
01/08/2025
๐ฅฅ:๐ผ์๋ฆฌ๐ผ:๐ฅฅ
03/05/2025
then why did you assume I was weak?
Zoro: Because you are a girl.
and that means I'm weak?
From the memory
8 Memories