note 82838 modified in function.PDO-lastInsertId by danbrown
| From: | danbrown@php.net | Date: | Fri, 28 Nov 2008 21:33:08 +0000 |
| Subject: | note 82838 modified in function.PDO-lastInsertId by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-147246@lists.php.net to get a copy of this message | ||
It should be noted here at this function will not display the correct ID if issuing ON DUPLICATE KEY
UPDATE.
Example on a new Row:
<?php
$sql = "INSERT INTO city (
city) VALUES ('Paris') ON DUPLICATE KEY UPDATE
city = 'Paris";
$dbh->query($sql);
echo $dbh->lastInsertId();
?>
Above displays: 1
Expected display: 1
Example on an existing row that gets updated:
<?php
$sql = "INSERT INTO city (city) VALUES ('Paris') ON DUPLICATE KEY UPDATE
city = 'Paris";
$dbh->query($sql);
echo $dbh->lastInsertId();
?>
Above displays: 2
Expected display: 1 (since no new records were inserted)
You'll have to work around this.
--was--
It should be noted here at this function will not display the correct ID if issuing ON DUPLICATE KEY
UPDATE.
Example on a new Row:
<?php
$sql = "INSERT INTO city (city) VALUES ('Paris') ON DUPLICATE KEY UPDATE
city = 'Paris";
$dbh->query($sql);
echo $dbh->lastInsertId();
?>
Above displays: 1
Expected display: 1
Example on an existing row that gets updated:
<?php
$sql = "INSERT INTO city (city) VALUES ('Paris') ON DUPLICATE KEY UPDATE
city = 'Paris";
$dbh->query($sql);
echo $dbh->lastInsertId();
?>
Above displays: 2
Expected display: 1 (since no new records were inserted)
You'll have to work around this.
http://php.net/manual/en/function.PDO-lastInsertId.php