note 61340 deleted from function.mysql-insert-id by cmb
| From: | cmb@php.net | Date: | Sun, 16 Feb 2020 13:30:52 +0000 |
| Subject: | note 61340 deleted from function.mysql-insert-id by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-214116@lists.php.net to get a copy of this message | ||
Note Submitter: john k.
----
Be careful when using "insert ignore". If the unique index already exists, the record will
not be added, but it WILL give you the id of the next auto_increment value even though it
didn't create it.
<?php
$sql = "insert ignore into sometable set num=10";
mysql_query($sql) or die();
echo mysql_affected_rows()."<br>";
echo mysql_insert_id()."<br><br>";
// same record, database is unique on 'num'
$sql = "insert ignore into sometable set num=10";
mysql_query($sql) or die();
echo mysql_affected_rows()."<br>";
echo mysql_insert_id()."<br><br>";
?>
would give:
1
116372
0
116373