Generate Unique Codes


I am working on a personal project which is to generate a set of unique codes and store them in a database. The concept of the project is for me to be able to determine the numbers of codes I want to generate and insert each of them into the database. I have been able to come up with something with the help of a tutorial which is working but not inputting into database and it allows one generation at a time. Please see my code below

$username = "root";
$password = "password";
$hostname = "localhost";
$database = "gencode";
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
$selected = mysql_select_db($database,$dbhandle)
or die("Could not select $database");

$unique_ref_length = 11;

$unique_ref_found = false;

$possible_chars = "23456789ABCDFGHJKLMNPQRSTWXYZ";

while (!$unique_ref_found) {

	$unique_ref = "";
	$i = 0;

	while ($i < $unique_ref_length) {

		$char = substr($possible_chars, mt_rand(0, strlen($possible_chars)-1), 1);
		$unique_ref .= $char;

	$query = "SELECT `order_ref_no` FROM `orders`
		      WHERE `order_ref_no`='".$unique_ref."'";
	$result = mysql_query($query) or die(mysql_error().' '.$query);
	if (mysql_num_rows($result)==0) {

		$unique_ref_found = true;


echo 'Our unique reference number is: '.$unique_ref;

Your assistance will be greatly appreciated.

//generate key
$uniqueString = md5(microtime(true));
//$uniqueString = new MongoId(); //I like this one, its used for mongodb, but it's useful here :)

// database connection
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
$q = $conn->prepare($sql);
$q->execute("INSERT INTO orders (order_ref_no) values ('$uniqueString')");

echo "Our reference number is $uniqueString";

Hello thanks for your response. I have tried your solution but doesn’t seems to work. I have even tried to tweak the insert but its not working pls any other suggestion. Thanks

I’m not sure what @K_Wolfe ; was doing when he wrote the code, but he mixed a few PDO strategies causing the code not to work.

//generate key
$uniqueString = md5(microtime(true));
//$uniqueString = new MongoId(); //I like this one, its used for mongodb, but it's useful here :)
// database connection
$conn = new PDO("mysql:host=$hostname;dbname=$database",$username,$password);
$q = $conn->prepare("INSERT INTO orders (order_ref_no) values (':uniqueString')");
$q->execute(array(':uniqueString' => $uniqueString));
echo "Our reference number is $uniqueString";

The above should be closer.

Worse case scenario, remove your while loops and replace them with to generate a unique string

$uniqueString = md5(microtime(true));


I have mentioned few steps below which are simple, useful and which might help you in solving the problem or at least identifying the problem.

  1. Try to insert any one unique code by UI interface (like we have in phpmyadmin)
  2. If that worked, then copy the query and save that query in a text file.
  3. Print your query which you are trying to fire on database and check the difference between the two.
  4. Either there might be some problem in query or might be the problem with the field limit.

I am not sure whether you have tried the above mentioned steps or not. If not please try them, it might help you.

Ya that makes two of us. :goof:

@cpradio, thanks for the assistance. works fine and insert into the db.

Although, I am planning on generating multiple codes, I was wondering if a while loop will be useful for that purpose. something like

$i = 0;
$j = 0;

while ($j < $no_of_code) {

    while ($i &lt; $unique_ref_length) {



Although I have not tried this as i am not presently on my dev pc, I just want to know what you think of it.