note 105580 added to pdo.lastinsertid
| From: | warezthebeef at gmail dot com | Date: | Tue, 30 Aug 2011 06:04:01 +0000 |
| Subject: | note 105580 added to pdo.lastinsertid | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-182818@lists.php.net to get a copy of this message | ||
If you're accessing MSSQL/SQL Server 2008 R2 (or higher) from Linux via FreeTDS there's a
slightly neater way of getting the last insert ID than the solution(s) outlined below.
The specific SQL involved is outlined here:
http://msdn.microsoft.com/en-us/library/ms177564.aspx
So for example, with a table containing the two columns (product_id, product_name) where product_id
is a uniqueidentifier or something similar you could do the following.
<?php
// Assume $dbh connection handle is already established
$sql = "INSERT INTO product (product_name) OUTPUT INSERTED.product_id VALUES (?)";
$sth = $dbh->prepare($sql);
$sth->execute(array('widgets'));
$temp = $sth->fetch(PDO::FETCH_ASSOC);
?>
Then $temp will contain an array like:
Array
(
[product_id] => E1DA1CB0-676A-4CD9-A22C-90C9D4E81914
)
Just be warned that there are some issues relating to how uniqueidentifier columns are handled by
PDO_DBLIB/FreeTDS depending on the TDS version you choose that have only been fixed as of PHP 5.3.7.
Information regarding this and the patch can be found here:
https://bugs.php.net/bug.php?id=54167
----
Server IP: 69.147.83.197
Probable Submitter: 210.48.113.14
----
Manual Page -- http://www.php.net/manual/en/pdo.lastinsertid.php
Edit -- https://master.php.net/note/edit/105580
Del: integrated -- https://master.php.net/note/delete/105580/integrated
Del: useless -- https://master.php.net/note/delete/105580/useless
Del: bad code -- https://master.php.net/note/delete/105580/bad+code
Del: spam -- https://master.php.net/note/delete/105580/spam
Del: non-english -- https://master.php.net/note/delete/105580/non-english
Del: in docs -- https://master.php.net/note/delete/105580/in+docs
Del: other reasons-- https://master.php.net/note/delete/105580
Reject -- https://master.php.net/note/reject/105580
Search -- https://master.php.net/manage/user-notes.php