DMXzone Database Updater PHP Support Product Page
Answered
Generate Unique Random Number
Asked 27 Jan 2014 02:32:57
1
has this question
27 Jan 2014 02:32:57 Brad Lawryk posted:
Not sure if this is the place to ask this or not but since I am using the DB Connector you may be able to point me in the right direction. Or better yet maybe there is a better way with DMX Extensions?Basically I am generating a unique 10 digit number to be entered into the database. Below is the code I am using with comments so you can see what I am doing. I can't seem to rap my head around how to do the check to see if number exists and if it does run the generator again.
<?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() { GLOBAL $connPGES; $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'", $connPGES) 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 ?>
Replies
Replied 28 Jan 2014 12:36:47
28 Jan 2014 12:36:47 Teodor Kuduschiev replied:
Hi Brad,
Unfortunately this is not possible with the HTML5 Data Bindings extension.
Unfortunately this is not possible with the HTML5 Data Bindings extension.