Jump to content

Recommended Posts

Hello,

 

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;

 $i++;

}


$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.

Link to comment
https://forums.phpfreaks.com/topic/274577-generate-unique-codes/
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.