waddledoo Posted December 3, 2011 Share Posted December 3, 2011 I am trying to place a unique number into a mysql table. Currently, my code generates a random number, then is supposed to scan through the table for that number. If the code finds that number already in the table, it generates a new random number and repeats the process. I have commented my code for the purpose of this help forum: $result = mysqli_query($link,"SELECT * FROM testTable"); do { $end = true; //prepares end of loop $idNum = rand(1,10); //rand(1,999999); <-- for testing purposes I have reduced the number generated $idNumTx = (string)$idNum; //loop through the rows while ($row = mysqli_fetch_assoc($result)) { if ($row['idNum'] = $idNum) //check if the random number equal to this row { $end = false; //prep end of loop repeat echo $idNumTx; //display rand number that failed for testing purposes echo " NO! "; //display error for testing purposes } } } while(!$end); I know I must be doing something wrong, as when I run this, it runs the if statement within the while loop always executes, and I get an output like: 1 NO! 1 NO! 1 NO! 1 NO! 1 NO! 1 NO! 5 Win Win is when it places the value before it, in this case 5, into the table. However, the value of 5 might already be in the table and it doesn't seem to matter. I execute the code multiple times, and it seems to increase the number of "# NO!" almost (but not every) time. However, each time ALL of the "# NO!" are the same #, and the "# Win" just seems to be random (as it should be, but not unique). Checking the table after shows me random numbers between 1 and 10 (as it should) in the correct field, but the are not unique. (Ex/ Both row 1 and 5 could have the same value, say 6) I'm hopefully doing something simple wrong, so someone please point it out to me Quote Link to comment https://forums.phpfreaks.com/topic/252378-php-mysql-table-loop/ Share on other sites More sharing options...
waddledoo Posted December 3, 2011 Author Share Posted December 3, 2011 I found a better way to accomplish what I wanted. I still don't understand what was wrong with my previous code, but the new code works perfectly: do { $end = true; $idNum = rand(1,10);//rand(1,999999); $idNumTx = (string)$idNum; $testExists = mysqli_query($link,"SELECT idNum FROM test3 where idNum='$idNum'"); $num = mysqli_num_rows($testExists); if ($num!=0) { $end = false; } } while(!$end); This code now queries the random number directly from the table, and will only complete the loop if the query returns nothing (if the #ofrows returned is 0) This thread can be marked as solved, or deleted. Quote Link to comment https://forums.phpfreaks.com/topic/252378-php-mysql-table-loop/#findComment-1293884 Share on other sites More sharing options...
PFMaBiSmAd Posted December 3, 2011 Share Posted December 3, 2011 Two == signs is a comparison operator. One = sign is an assignment operator. Your first code was assigning $idNum to $row['idNum'] in the if(){} statement, and then testing if the value that was assigned evaluated to a true or false. Quote Link to comment https://forums.phpfreaks.com/topic/252378-php-mysql-table-loop/#findComment-1293889 Share on other sites More sharing options...
waddledoo Posted December 3, 2011 Author Share Posted December 3, 2011 Ah thankyou. More and more I see PHP is like C++. I was used to coding in python hehe Quote Link to comment https://forums.phpfreaks.com/topic/252378-php-mysql-table-loop/#findComment-1293896 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.