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