Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2004
    Posts
    82

    Unanswered: Duplicates and Databases

    Hi I wanted to know how generate a random number check if the number already exists in the mysql database and if so generate another random number and check that etc until there is no duplicate. My current code:

    Somehow say:

    PHP Code:
    $random = (11111,999999);

    $selectdupe mysql_query("SELECT * FROM table WHERE numbers = '$random'")
    $numberofrows mysql_num_rows($selectdupe);
    if (
    $numberofrows 0){ ##If there is a duplicate
    ##GENERATE ANOTHER NUMBER
    } else {
    ##CONTINUE

    However this code would have to continue generating and then checking over and over and over again...if there a while loop I can do here...?


    Thanks for any help,

    Jordan
    Last edited by jwman; 03-18-06 at 13:10.

  2. #2
    Join Date
    Apr 2005
    Location
    Baltimore, MD
    Posts
    297
    Are you trying to generate a uniquely random number or select a random row?

    To generate a uniquely random number, just repeat the process until the number you come up with is not in your collection (use a 'while' loop.)
    PHP Code:
    $random 0;
    $doLoop true;
    while(
    $doLooop) {
       
    $random mt_rand(11111,999999);

       
    $selectdupe mysql_query("SELECT * FROM table WHERE numbers = '$random'")
       
    $doLoop = (mysql_num_rows($selectdupe) > 0);

    To select a random row from the database do:
    Code:
    select count(*) AS x from table
    Get random number y between 1 and x.
    select xxxx from table limit y,1
    from: Colin Guthrie's comment at http://www.greggdev.com/web/articles.php?id=6

  3. #3
    Join Date
    Aug 2004
    Posts
    82
    Thank you, the top code is definately a step forward...however I forgot to mention that I need to be able to write the new, unique number to the database and echo it to the user...I cannot seem to work out how to...if possible...with the code you supplied.

    Any help here?


    Jordan

  4. #4
    Join Date
    Apr 2005
    Location
    Baltimore, MD
    Posts
    297
    PHP Code:
    $random 0;
    $doLoop true;
    while(
    $doLooop) {
       
    $random mt_rand(11111,999999);

       
    //rather than SELECT *, just use a single field or modify code to use COUNT()...
       //since you're not using the data, * will just return useless information and hog resources
       
    $selectdupe mysql_query("SELECT * FROM table WHERE numbers = '$random'")
       
    $doLoop = (mysql_num_rows($selectdupe) > 0);
    }  
    //once we exit the loop, $random will have a unique random value
    echo $random;
    $sql "INSERT INTO table(..., numbers) VALUES (..., '$random')";
    mysql_query($sql); 

  5. #5
    Join Date
    Aug 2004
    Posts
    82
    Thanks very much that did the trick

Posting Permissions

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