title: How to find the next available integer in MySQL table using PHP
body: <p>I know <strong>auto_increment</strong> is the way to go but I can not use auto_increment feature since the column in my table might repeat, its not unique. When I insert a new row to a table I need a way to find the next available spot to insert it.</p>
  
  <p>For example table structure:</p>
  
  <pre><code> Primary Key = (ID, UserID)
   ID   UserID 
   3    6
   3    1
   1    3
  </code></pre>
  
  <p>Now when i do insert query i want to isert it at <strong>ID = 2</strong> and not <strong>4</strong>. With <strong>auto_increment</strong> it gives me <strong>4</strong></p>
  
  <p>Is there a solution without using the loop in PHP? So far what i have is I fetch all rows into array and then find the next available digit in ID. Is it possible to do this without fetching all rows in PHP and just doing it on MySQL query ?</p>