While loop to check if a value exist in my table

Hi,

I have a problems with a loop, cant finish it.
I have a function which create a random string, this is my "sitekey" which needs to be unique.

The loop need to create and check if the key exist, the loop will stop as soon it have create a key which does not exist in the table.

This is what I have atm:

// Create unique SiteKey
                  while( 1 == 1 ) {
                        
                        $sitekey = randomstring(25);
                        
                        $result = mysql_query("SELECT sitekey FROM sites WHERE sitekey = '$sitekey' ");
                        $count = mysql_num_rows($result);
                        
                        if($count == 0)  {
                        
                              return $sitekey;
                              end;
                        }
                  }
kgp43Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

teedo757Commented:
while( $count != 0) {
                       
                        $sitekey = randomstring(25);
                       
                        $result = mysql_query("SELECT COUNT FROM sites WHERE sitekey = '$sitekey' ");
                        $count = mysql_num_rows($result);
                       
                                               }
                            return $sitekey;
                              end;
                  }
0
kgp43Author Commented:
This seems to work.
500 points to the first one who confirm :)


// Create unique SiteKey
                  while( true ) {
                        
                        $sitekey = randomstring(1);
                        
                        $result = mysql_query("SELECT sitekey FROM sites WHERE sitekey = '$sitekey' ");
                        $count = mysql_num_rows($result);
                        
                        if($count == 0)  {
                        
                              echo $sitekey;
                              return true;
                        }
                  }
0
teedo757Commented:
Make sure to initialize the variable before starting the loop

$count = 9
0
teedo757Commented:
Just test it to confirm

// Create unique SiteKey
                  while( true ) {
                       
                        $sitekey = randomstring(1);
                       
                        $result = mysql_query("SELECT sitekey FROM sites WHERE sitekey = '$sitekey' ");
                        $count = 1; // This would be if a match was found
                       
                        if($count == 0)  {
                       
                              echo $sitekey;
                              return true;
                        }
                  }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.