title: PHP MYSQL set AUTO INCREMENT to MAX + 1
body: <p>I have a PHP file that inserts data from a CSV file into the MYSQL DB using the LOAD DATA INFILE sql function.</p>
  
  <p>If the CSV data is a duplicate it is not inserted because of the DB table indexing rules (unique).</p>
  
  <pre><code>$sql = "LOAD DATA LOW_PRIORITY LOCAL INFILE 
         '" . $makes_file . "' IGNORE 
         INTO TABLE make 
         FIELDS TERMINATED BY ',' 
         LINES TERMINATED BY '\n' (make, img_url)";
  
  $link-&gt;query($sql) or die(mysqli_error($link));
  </code></pre>
  
  <p>For some reason the Auto Increment of the table is not correct from this process. </p>
  
  <p>So I have used the following code to correct this. </p>
  
  <pre><code>$get_max = $link-&gt;query("SELECT max(id) as max FROM `make` LIMIT 1");
  while ($r = $get_max-&gt;fetch_assoc()) {
      $link-&gt;query("ALTER TABLE  `make` AUTO_INCREMENT = " . ($r['max'] + 1)) or 
      die(mysqli_error($link));
  }
  </code></pre>
  
  <p>So if anybody knows:</p>
  
  <ol>
  <li>Why the Auto Increment is incorrect from the LOAD DATA sql 
  or</li>
  <li>If there is a 'prettier' way to set the auto increment to the max(id) +1</li>
  </ol>
  
  <p>Thanks</p>