Note that there are some explanatory texts on larger screens.

plurals
  1. POgenerate get next unique number from mysql ID
    primarykey
    data
    text
    <p>I need to generate a unique extension for each user (this is separate from the ID but works much the same) the number should be 100 or greater and can be overwritten/custom set. Right now I am taking the next id and if it's less than 100 add 100. </p> <p>So if the next id is 5 the number would be 105 but it the next id is 105 the number would just be 105. The problem is that because i'm letting the user pick their own extension if a user choose 105 before I want to make it automatically jump to the next number in this case 106. Now if there is 105, 106, and 108 I would want to jump to 107 then jump to 109. Here is the code I'm using to generate the numbers. I think the problem is with my while loop. I'm not sure how to make it keep on checking for unique numbers. </p> <p>Here is the code, I'm sure I'm overcomplicated things A LOT. </p> <pre><code>$result = mysql_query("SELECT MAX(id) FROM USERS"); $row = mysql_fetch_row($result); $sql_extention = intval($row[0]); //make sure it's at least 100 $extension = ($sql_extension &lt; 100) ? $sql_extension+100 : $sql_extension; //check to see if the extention is in use $qry = "SELECT `extention` FROM users WHERE extention = '$extention'"; $result2 = mysql_query($qry); //if the extention is in use then find the next available one (this isn't currently working) if($result2) { //get all results greater or equal to our extention $qry3 = "SELECT `id`,`extention` FROM admin_users WHERE extention &gt;= '$extention'"; $result3 = mysql_query($qry3); //this loop needs to be rewritten somehow to get the next number by checking if the next number exist if not return that as the extention $new_extention = $extention+1; while($extention_data = mysql_fetch_array($result3)) { if($new_extention != $extention_data['extention']+1) { $extention = $new_extention; } $new_extention++; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload