Jump to content

Checking uniqueness against database


Soup

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.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.