Jump to content

Help Generating 6 Digit Unique Numeric Code!


iPixel

Recommended Posts

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!

Link to comment
Share on other sites

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();
?>

 

Link to comment
Share on other sites

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 !!!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.