SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Feb 2013
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Generate Unique Codes

    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

    PHP Code:
    $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_charsmt_rand(0strlen($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.

  2. #2
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,180
    Mentioned
    65 Post(s)
    Tagged
    2 Thread(s)
    Code PHP:
    //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";

  3. #3
    SitePoint Member
    Join Date
    Feb 2013
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,122
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    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.

    PHP Code:
    //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
    PHP Code:
    $uniqueString md5(microtime(true)); 

  5. #5
    SitePoint Enthusiast
    Join Date
    May 2011
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello,

    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.
    PHP Programming Itís more than just writing code

  6. #6
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,180
    Mentioned
    65 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by cpradio View Post
    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.
    Ya that makes two of us.

  7. #7
    SitePoint Member
    Join Date
    Feb 2013
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @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 < $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.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •