iPixel Posted March 9, 2011 Share Posted March 9, 2011 Below is my code... I'm new to OOP so forgive me if it's poorly designed. What i'm trying to do is make sure that asset_key never get's duplicated. So when time comes to insert a new record, i can rest assures that asset_key will not duplicate. For some reason $random_number never get's back a value; <?php class Randomize { function createRN() { $num = rand(100000,999999); $sql = "SELECT * FROM mer_asset_header WHERE asset_key = '$num'"; $go = oci_parse($conn, $sql); oci_execute($go); $count = oci_num_rows($go); if($count = 1) $repeat = Randomize::createRN(); else return $num; } } $random_number = new Randomize; $random_number->createRN(); ?> I should also mention, that if the query finds that asset_key, it should also run createRN() again untill it gets a unique one. Thanks for the help in advance! Quote Link to comment https://forums.phpfreaks.com/topic/230123-help-generating-6-digit-unique-numeric-code/ Share on other sites More sharing options...
AbraCadaver Posted March 9, 2011 Share Posted March 9, 2011 if($count = 1) Maybe: if($count == 1) Also, you might consider mt_rand(). Quote Link to comment https://forums.phpfreaks.com/topic/230123-help-generating-6-digit-unique-numeric-code/#findComment-1185118 Share on other sites More sharing options...
iPixel Posted March 9, 2011 Author Share Posted March 9, 2011 if($count = 1) Maybe: if($count == 1) Also, you might consider mt_rand(). Unfortunately no . Quote Link to comment https://forums.phpfreaks.com/topic/230123-help-generating-6-digit-unique-numeric-code/#findComment-1185119 Share on other sites More sharing options...
Zane Posted March 9, 2011 Share Posted March 9, 2011 $return = $num; if($count >= 1) $return = Randomize::createRN(); else return $return; The main thing wrong with your code is your use of $repeat. It is never declared beforehand AND you never use it when you do declare it. Quote Link to comment https://forums.phpfreaks.com/topic/230123-help-generating-6-digit-unique-numeric-code/#findComment-1185121 Share on other sites More sharing options...
AbraCadaver Posted March 9, 2011 Share Posted March 9, 2011 Sorry, that was the first problem I saw, but there is more: if($count == 1) { $num = Randomize::createRN(); } return $num; Quote Link to comment https://forums.phpfreaks.com/topic/230123-help-generating-6-digit-unique-numeric-code/#findComment-1185123 Share on other sites More sharing options...
iPixel Posted March 9, 2011 Author Share Posted March 9, 2011 I did this but still nothing... i dont understand why am i using $num = Randomize::createRN(); <?php class Randomize { function createRN() { $num = rand(100000,999999); $sql = "SELECT * FROM mer_asset_header WHERE asset_key = '$num'"; $go = oci_parse($conn, $sql); oci_execute($go); $count = oci_num_rows($go); if($count == 1) { $num = Randomize::createRN(); } return $num; } } $random_number = new Randomize; $random_number->createRN(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/230123-help-generating-6-digit-unique-numeric-code/#findComment-1185127 Share on other sites More sharing options...
Zane Posted March 9, 2011 Share Posted March 9, 2011 i dont understand why am i using $num = Randomize::createRN(); So the function runs recursively (again and again) until $count != 1 ... or in the case of my example... until count is less than 1 Quote Link to comment https://forums.phpfreaks.com/topic/230123-help-generating-6-digit-unique-numeric-code/#findComment-1185129 Share on other sites More sharing options...
iPixel Posted March 9, 2011 Author Share Posted March 9, 2011 Nevermind, i got it working... i just made a noobie mistake and was calling echo $random_number later on in the code after $random_number->createRN(); so i just did $random = $random_number->createRN() and echo $random whenever neede. Thanks !!! Quote Link to comment https://forums.phpfreaks.com/topic/230123-help-generating-6-digit-unique-numeric-code/#findComment-1185130 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.