EmperorJazzy Posted April 1, 2011 Share Posted April 1, 2011 Howdy, I'm utilising a random number as an ID for each new object created. At the creation stage (input to database tables), I'm wishing to check that the new random number doesn't already exist. Here is my code; Field 'ListID' is a varchar field while ($listid='') { $listtest = rand(1000000000,9999999999); $query = "SELECT ListID FROM tblUserList WHERE ListID='" . $listtest . "'"; $result = mysqli_query($dbc, $query) or die('Error checking ListID - ' . $query); $row_cnt = mysqli_num_rows($result) If ($row_cnt=0) { $listid=$listtest; } } The code is not executing; essentially, there are no error returns and the page continues to sit idly waiting for something to return. Is this loop ot logical? Quote Link to comment https://forums.phpfreaks.com/topic/232430-eternal-loop/ Share on other sites More sharing options...
Nuv Posted April 1, 2011 Share Posted April 1, 2011 Can you please Try this <?php If ($row_cnt == 0) // use double == here { $listid=$listtest; } ?> One = is used for assigning values and == is used for comparing Quote Link to comment https://forums.phpfreaks.com/topic/232430-eternal-loop/#findComment-1195603 Share on other sites More sharing options...
EmperorJazzy Posted April 1, 2011 Author Share Posted April 1, 2011 So... <?php while ($listid='') { $listtest = rand(1000000000,9999999999); $query = "SELECT ListID FROM tblUserList WHERE ListID='" . $listtest . "'"; $result = mysqli_query($dbc, $query) or die('Error checking ListID - ' . $query); $row_cnt = mysqli_num_rows($result) If ($row_cnt==0) { $listid=$listtest; } } ?> Should I also use the == for the while statement? <?php while ($listid=='') ?> Quote Link to comment https://forums.phpfreaks.com/topic/232430-eternal-loop/#findComment-1195609 Share on other sites More sharing options...
Nuv Posted April 1, 2011 Share Posted April 1, 2011 No don't use == for while. Only for if. Try it and check if it works. (I am considering there is more to your code.) Quote Link to comment https://forums.phpfreaks.com/topic/232430-eternal-loop/#findComment-1195616 Share on other sites More sharing options...
DavidAM Posted April 1, 2011 Share Posted April 1, 2011 One = is used for assigning values and == is used for comparing YES! Use == for that while loop as well. Quote Link to comment https://forums.phpfreaks.com/topic/232430-eternal-loop/#findComment-1195654 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.