Jump to content

Recommended Posts

I'm trying to create a unique 8 digit id and have it check against the database so there can be no duplicate. It stores the id fine but the check aspect doesn't seem to be functioning since i have gotten the same number twice on test runs.  Here is what i have so far, any help would be great.

 

$loop=true;

while($loop) {

  $rand = rand(10000000,99999999);

  if(mysql_query("SELECT * FROM $table_name WHERE `rand` = '{$rand}' LIMIT 1")) $loop=false;

}

$query ="INSERT into $table_name (id, rand) values ('id','$rand')";

if (mysql_db_query ($db, $query))

{

    print ("Your information was successfully uploaded!<br />\n");

}

else

{

    print ("Sorry! There was an error in uploading your information!<br />\n");

}

mysql_close ($link);

?>

<html>

<head></head>

<body>

<?php

echo $rand;

?>

</body>

</html>

Link to comment
https://forums.phpfreaks.com/topic/177665-checking-uniqueness-against-database/
Share on other sites

if(mysql_query("SELECT * FROM $table_name WHERE `rand` = '{$rand}' LIMIT 1"))

The above line of code just tests if the query executed without errors. It does not test if any row(s) were returned in the result set. You need to use mysql_num_rows() to find out how many rows are in a result set.

 

 

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.