...

View Full Version : Checking if row exists in database, and if it does, try again with another string?



nikee
03-22-2010, 03:07 PM
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 :rolleyes:

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:



//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);
}
}Unfortunately it isn't working :(


Thanks in advance. :thumbsup:
-Nike

mlseim
03-22-2010, 03:13 PM
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.

masterofollies
03-22-2010, 03:15 PM
Use uniqid() it will create a unique ID each time. You can add a prefix to it also if needed.

nikee
03-22-2010, 03:21 PM
Ohh, why didn't i think of that? *sigh* haha!

So, something like this would do?


$threeFirst = rand(100,999);
$unicId = date(YmdHisu) . $threeFirst;//that's like: 20100322152108654321 + those three last ones... damn! :D
@masterofollies (http://codingforums.com/member.php?u=22982); i already have another column that auto increases, if that's what you mean? :)

mlseim
03-22-2010, 04:22 PM
auto-increment ID is incrementing.
unique ID is random, but never repeating (I think that statement is correct?)

or, using date() or time() ... the final result is to get a unique, non-repeating number.

masterofollies
03-22-2010, 05:24 PM
unique ID is random, but never repeating (I think that statement is correct?)


Correct, if it matches it changes it's self. So you never have to worry about a double.

nikee
03-22-2010, 05:54 PM
That sounds pretty good! It doesn't seem to want to be applied though :(

Any idea what's up?
(sorry most of it is in swedish)
http://i.imagehost.org/0948/28_11.png

MattF
03-22-2010, 05:59 PM
It tells you what the error is. You're trying to insert a second value with a value of 0.

DJCMBear
03-22-2010, 06:06 PM
empty all the id numbers before adding the auto int

nikee
03-22-2010, 06:31 PM
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 :$

nikee
03-22-2010, 06:36 PM
Ohh noo!

It's actually not working as expected, at all!

When writing to the database, i get this error:


Duplicate entry '0' for key 'unique_id'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.

DJCMBear
03-22-2010, 07:32 PM
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.

masterofollies
03-22-2010, 07:48 PM
Making a forum will take a good year of work to make. I suggest download a forum and customizing it.

nikee
03-22-2010, 08:06 PM
@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.

MattF
03-22-2010, 08:42 PM
@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.

nikee
03-22-2010, 08:52 PM
You're right, i wouldn't say my forum is 100% bugfree, nor 100% secure. But i did my best, and for coding something this "large" for the first time, in less than two weeks, i'm pretty satisfied with the result.

Thanks! :)

masterofollies
03-22-2010, 09:53 PM
@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.

Anyone can make a quick mini forum with no features. But a forum like we are talking on now? Good luck. It will take a year and probably 3 coders. :thumbsup:

MattF
03-22-2010, 09:56 PM
Anyone can make a quick mini forum with no features. But a forum like we are talking on now?

He? never mentioned creating a bloated forum, merely a forum. That's anything from a simple message board, i.e: post, reply etc upwards.

nikee
03-22-2010, 10:11 PM
To give you an idea of how "advanced" it is, take a look at the link below.

http://nike1.se/forum/

I wouldn't call it "a mini forum with no features", it does have a few fancy functions under the hood. :)

Really? One entire year, with three coders?

EDIT: The Settings pic doesn't contain all settings as they didn't fit in the screenshot... And i do have a few things i would like to improve. And one feature i WILL add whenever i get access to the schoolserver, is thread subscription, so you can get notified through email when you get a new reply. I'm also going to add thread ratings and a search form. :)

masterofollies
03-23-2010, 01:19 AM
Yeah that is super basic. When someone says "I am making a forum" I automatically think of a huge forum like this one, with 40 - 60 files.

But yeah yours would be quick and simple.

nikee
03-23-2010, 11:34 AM
I can't say my forum is as good as this one, or have as much features, but i still think it's pretty good. Don't judge it from it's design, it's what happens behind it that's really important. 23 files is what i've got, so far. And remember i barely knew anything before i started - and i've mostly been working with it in school (3hrs a week).

masterofollies
03-23-2010, 03:32 PM
I think your doing a good job. Keep at it. We just didn't understand each other.

nikee
03-23-2010, 07:43 PM
Thanks!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum