Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-22-2010, 03:07 PM   PM User | #1
nikee
Regular Coder

 
Join Date: Jan 2009
Posts: 173
Thanks: 5
Thanked 12 Times in 11 Posts
nikee is an unknown quantity at this point
Question 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);
    }

Unfortunately it isn't working


Thanks in advance.
-Nike

Last edited by nikee; 03-22-2010 at 03:10 PM..
nikee is offline   Reply With Quote
Old 03-22-2010, 03:13 PM   PM User | #2
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,046
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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.
mlseim is offline   Reply With Quote
Old 03-22-2010, 03:15 PM   PM User | #3
masterofollies
Senior Coder

 
Join Date: May 2005
Posts: 2,137
Thanks: 96
Thanked 72 Times in 72 Posts
masterofollies can only hope to improve
Use uniqid() it will create a unique ID each time. You can add a prefix to it also if needed.
__________________
Rowsdower! has accused me of having mental problems, and the administrator allowed it. What a great forum huh?
masterofollies is offline   Reply With Quote
Old 03-22-2010, 03:21 PM   PM User | #4
nikee
Regular Coder

 
Join Date: Jan 2009
Posts: 173
Thanks: 5
Thanked 12 Times in 11 Posts
nikee is an unknown quantity at this point
Ohh, why didn't i think of that? *sigh* haha!

So, something like this would do?

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

Last edited by nikee; 03-22-2010 at 03:23 PM..
nikee is offline   Reply With Quote
Old 03-22-2010, 04:22 PM   PM User | #5
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,046
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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.
mlseim is offline   Reply With Quote
Old 03-22-2010, 05:24 PM   PM User | #6
masterofollies
Senior Coder

 
Join Date: May 2005
Posts: 2,137
Thanks: 96
Thanked 72 Times in 72 Posts
masterofollies can only hope to improve
Quote:
Originally Posted by mlseim View Post
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.
__________________
Rowsdower! has accused me of having mental problems, and the administrator allowed it. What a great forum huh?
masterofollies is offline   Reply With Quote
Old 03-22-2010, 05:54 PM   PM User | #7
nikee
Regular Coder

 
Join Date: Jan 2009
Posts: 173
Thanks: 5
Thanked 12 Times in 11 Posts
nikee is an unknown quantity at this point
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)
nikee is offline   Reply With Quote
Old 03-22-2010, 05:59 PM   PM User | #8
MattF
Senior Coder

 
Join Date: Jul 2009
Location: South Yorkshire, England
Posts: 2,322
Thanks: 6
Thanked 304 Times in 303 Posts
MattF will become famous soon enoughMattF will become famous soon enough
It tells you what the error is. You're trying to insert a second value with a value of 0.
MattF is offline   Reply With Quote
Old 03-22-2010, 06:06 PM   PM User | #9
DJCMBear
Senior Coder

 
DJCMBear's Avatar
 
Join Date: Mar 2010
Location: United Kindom
Posts: 1,173
Thanks: 14
Thanked 136 Times in 136 Posts
DJCMBear is on a distinguished road
empty all the id numbers before adding the auto int
DJCMBear is offline   Reply With Quote
Old 03-22-2010, 06:31 PM   PM User | #10
nikee
Regular Coder

 
Join Date: Jan 2009
Posts: 173
Thanks: 5
Thanked 12 Times in 11 Posts
nikee is an unknown quantity at this point
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 is offline   Reply With Quote
Old 03-22-2010, 06:36 PM   PM User | #11
nikee
Regular Coder

 
Join Date: Jan 2009
Posts: 173
Thanks: 5
Thanked 12 Times in 11 Posts
nikee is an unknown quantity at this point
Ohh noo!

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

When writing to the database, i get this error:

Code:
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.

Last edited by nikee; 03-22-2010 at 06:50 PM..
nikee is offline   Reply With Quote
Old 03-22-2010, 07:32 PM   PM User | #12
DJCMBear
Senior Coder

 
DJCMBear's Avatar
 
Join Date: Mar 2010
Location: United Kindom
Posts: 1,173
Thanks: 14
Thanked 136 Times in 136 Posts
DJCMBear is on a distinguished road
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 is offline   Reply With Quote
Old 03-22-2010, 07:48 PM   PM User | #13
masterofollies
Senior Coder

 
Join Date: May 2005
Posts: 2,137
Thanks: 96
Thanked 72 Times in 72 Posts
masterofollies can only hope to improve
Making a forum will take a good year of work to make. I suggest download a forum and customizing it.
__________________
Rowsdower! has accused me of having mental problems, and the administrator allowed it. What a great forum huh?
masterofollies is offline   Reply With Quote
Old 03-22-2010, 08:06 PM   PM User | #14
nikee
Regular Coder

 
Join Date: Jan 2009
Posts: 173
Thanks: 5
Thanked 12 Times in 11 Posts
nikee is an unknown quantity at this point
@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.
nikee is offline   Reply With Quote
Old 03-22-2010, 08:42 PM   PM User | #15
MattF
Senior Coder

 
Join Date: Jul 2009
Location: South Yorkshire, England
Posts: 2,322
Thanks: 6
Thanked 304 Times in 303 Posts
MattF will become famous soon enoughMattF will become famous soon enough
Quote:
Originally Posted by nikee View Post
@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.
MattF is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:36 AM.


Advertisement
Log in to turn off these ads.