DMXzone App Connect Support Product Page

Generate random number and insert into database

Asked 16 Mar 2017 17:32:52
1
has this question
16 Mar 2017 17:32:52 Brad Lawryk posted:
I need to generate a 10 digit random number, check that the number doesn't already exist, and put that number into a hidden field and then enter that into a new record with the rest of the form.

Possible with these extensions or do I have to do something custom?

Replies

Replied 16 Mar 2017 17:34:21
16 Mar 2017 17:34:21 Brad Lawryk replied:
Here is the code I used to use ... is there a way to use it with App Data Actions?


Quote<?php
// Start of random Id generator
$rand_number = gen_rand_number();
//echo “Current random number = $rand_number\n”;


// Generates random number, which does not exist in the `random_test` database table
// Returns null if random number not found after 1,000 tries. This is a safety factor
// to prevent endless looping of the PHP while() statement. Should never occur.

function gen_rand_number() {
$i = 1000;
while ($i— {
do {
$rand_number = mt_rand(0000000001,9999999999);
} while (strlen($rand_number) < 10); // Discard random numbers less that 10 digits by trying again
$result = mysql_query(“SELECT client_random FROM clients WHERE client_random=‘$rand_number’” or die(mysql_error());
// Random number, which does not exist in the database found.
if (mysql_num_rows($result) === 0)
return $rand_number;
}
return null;
}


// End random Id Generator
?>

Replied 17 Mar 2017 15:50:40
17 Mar 2017 15:50:40 Teodor Kuduschiev replied:
Hi Brad,

Why don't you add an GUID/UUID column in your database instead - it will generate an unique ID on creating a record.
Replied 17 Mar 2017 16:03:46
17 Mar 2017 16:03:46 Brad Lawryk replied:
There is one.

But this is a government project and it requires a random unique 10 digit ID for security purposes. The database itself is about 6 years old already has has about 15,000 records. To change the structure now would be impossible.

Reply to this topic