Checking if row exists in database, and if it does, try again with another string?
Hi.
To begin with, i just wanna say that i wasn't sure whether to post this in the PHP or MySQL forum. So if i posted it wrong, please move it
I need some help. As it says in the title what i'm trying to do is to generate a random number (a big one, since it has to be unic!), and then check if it's already used in that particular database table. IF it already is used, then generate a new random number and check again, and i want it to continue looping until it finds a number that is not used in the database.
Here's the code i currently have:
PHP Code:
//Generate a random number and store it in $unic_id $unic_id = rand(0,10000000000);
//try to select the number in $unic_id in the database table $sql = mysql_query('SELECT unic_id FROM users WHERE unic_id="' . $unic_id . '"'); $unicId_array = mysql_fetch_array($sql);
//Checks if $unicId_array has a value set to it, and if it does, generate a random number etc... if(isset($unicId_array['unic_id'])){ while(isset($unicId_array['unic_id'])){ //gör ett nytt random nummer $unic_id = rand(0,10000000000);
//Check if the new $unic_id exists $sql = mysql_query('SELECT unic_id FROM users WHERE unic_id="' . $unic_id . '"'); $unicId_array = mysql_fetch_array($sql); } }
If you take a random number that is like 6 numbers long,
and append it to time(), it will never be repeated. You
would not even have to check.
time(), which is UNIX time, is a 10 digit number.
Example, right now it's about: 1269215080
So you could append 3 numbers in front, 3 in back,
or 6 in front, or 6 in back ... no matter how you append
any more random numbers or letters, it will never be the same.
This UNIX timestamp ( 1269215080 ) only existed for 1 second,
and it will never exist again.
Yeah i saw the error, and i understood it. But what didn't make sense is that because the column doesn't exist, i can't empty them! I had 4 users registered (i'm making a forum if anybody's wondering), and i had to remove them all for it to work. I added it now and it works as expected!
There was however something else i was going to ask, but i kind of forgot it :$
So it doesn't seem to be getting an unique id after all, huh?
EDIT: If i manually set unique_id to something that doesn't exist, like 5 for example (since i only have one user, with the unique_id of 0), then it works. But if i just leave it blank, like '', then it i get that dis-liked error message.
from the picture you are setting the index to uniuqe and not setting the auto int which is just under it and if its an id the it would be a primary key in the index dropdown.
@DJCMBear, i already have a primary key, called just id. But i'll use the date() method instead.
@masterofollies, making a forum isn't THAT hard. Believe it or not, but i'm pretty much done with mine now! The reason i'm coding it myself is for two reasons; 1) It's a school work, so i have to do it, haha. And 2) It's fun and instructive! I've learned a few very important things while creating this forum. Before creating it, i didn't even know how to connect to a database, in fact, the only thing i knew in PHP was how to include another file, and how to work with really basic variables. It took me 2 weeks to finish it. I'd show you if i could, but i have it on localhost right now.
Oh and i've been using both vBulletin and IP board.
@masterofollies, making a forum isn't THAT hard. Believe it or not, but i'm pretty much done with mine now! The reason i'm coding it myself is for two reasons; 1) It's a school work, so i have to do it, haha. And 2) It's fun and instructive! I've learned a few very important things while creating this forum. Before creating it, i didn't even know how to connect to a database, in fact, the only thing i knew in PHP was how to include another file, and how to work with really basic variables. It took me 2 weeks to finish it.
He's referring to the fact that there is a big difference between coding something up that you can use as a forum and coding something up that is both secure, bug free, (as much as any software ever can be, that is), and fully functioning forum. The former is, as you mention, a simple enough task. I doubt there's any on here who would suggest taking less than several months, (absolute minimum), to achieve the latter, however.
Good to see someone knuckling down to producing something and enjoying doing it, however. Well done.