note 87589 modified in pdo.lastinsertid by danbrown

From: Date: Mon, 19 Jan 2009 20:45:55 +0000
Subject: note 87589 modified in pdo.lastinsertid by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-149481@lists.php.net to get a copy of this message
Workaround for the fact that MSSQL does not provide lastInsertId(). This is locale-independent by design. <?php $mixRc = false; try { // Issue a compound command, 2nd part outputs the inserted Id $strQuery = 'INSERT INTO t1 (f1,f2) VALUES(v1,v2); SELECT @@IDENTITY AS mixLastId'; // Yup, your eyes are ok, NOT exec but query!!! $objSth = $objDb->query($strQuery); $mixRc = (is_object($objSth) and $objSth->errorCode() == '00000'); } catch (PDOException $objException) { $pdoMsg = $objException->getMessage(); $pdoMsg = iconv("ISO-8859-1", "UTF-8", $pdoMsg); $strMessage = 'insertRecord: Failed ' . $strQuery . ', Error Message: ' . $pdoMsg; doLog(__FILE__, __LINE__, $strMessage); throw new core_exception_database($strMessage); } if ($mixRc === false) return false; // The compound command delivers a multi-rowset statement handle // Move past the first (invalid) rowset from the INSERT command $objSth->nextRowset(); // Pick up the first row of the rowset from "SELECT @@IDENTITY" $rowTd = $objSth->fetch(PDO::FETCH_NUM); if (!is_array($rowTd)) { doLog(__FILE__, __LINE__, 'insertRecord: $objSth->fetch() returns %s', gettype($rowTd)); return false; } $objSth->closeCursor(); $strLastRowId = trim($rowTd[0]); // trim() for trailing Nullbyte // Integers are returned stringified, format depends on locale // Generally ends with ",00" or ".00" - trim that off $strLastRowId = preg_replace('/[,.]0+$/', '', $strLastRowId); // Remove any remaining "." or "," for thousands $strLastRowId = preg_replace('/[,.]/', '', $strLastRowId); // A GUID, which contains no "," or ".", will be left unchanged return $strLastRowId; ?> --was-- Workaround for the fact that MSSQL does not provide lastInsertId() <?php $mixRc = false; try { // Issue a compound command, 2nd part outputs the inserted Id $strQuery = 'INSERT INTO t1 (f1,f2) VALUES(v1,v2); SELECT @@IDENTITY AS mixLastId'; // Yup, your eyes are ok, NOT exec but query!!! $objSth = $objDb->query($strQuery); $mixRc = (is_object($objSth) and $objSth->errorCode() == '00000'); } catch (PDOException $objException) { $pdoMsg = $objException->getMessage(); $pdoMsg = iconv("ISO-8859-1", "UTF-8", $pdoMsg); $strMessage = 'insertRecord: Failed ' . $strQuery . ', Error Message: ' . $pdoMsg; doLog(__FILE__, __LINE__, $strMessage); throw new core_exception_database($strMessage); } if ($mixRc === false) return false; // The compound command delivers a multi-rowset statement handle // Move past the first (invalid) rowset from the INSERT command $objSth->nextRowset(); // Pick up the first row of the rowset from "SELECT @@IDENTITY" $rowTd = $objSth->fetch(PDO::FETCH_NUM); if (!is_array($rowTd)) { doLog(__FILE__, __LINE__, 'insertRecord: $objSth->fetch() returns %s', gettype($rowTd)); return false; } $objSth->closeCursor(); $strLastRowId = $rowTd[0]; // An integer is returned as "nn,00" - catch and delete the ",00" $p = strpos($strLastRowId,','); if ($p !== false) $strLastRowId = substr($strLastRowId, 0, $p); // Remove all '.' $strLastRowId = str_replace('.', '', $strLastRowId); // A GUID, which contains no "," or ".", will be left unchanged return $strLastRowId; ?> http://php.net/manual/en/pdo.lastinsertid.php

« previous php.notes (#149481) next »