note 102614 added to pdo.lastinsertid

From: Date: Thu, 24 Feb 2011 15:48:54 +0000
Subject: note 102614 added to pdo.lastinsertid
Groups: php.notes 
Request: Send a blank email to php-notes+get-177296@lists.php.net to get a copy of this message
I think I get a nice solution in Postgres to get the ID using the RETURNING that comes with Postgress since version 8.2. In the example below, I add to my insert clause the "returning" along with the primary key of my table, then after the execute, I do a fetch getting an array with the value of the last inserted id. <? public function insert($employee){ $sqlQuery = "INSERT INTO employee(user_id,name,address,city) VALUES(:user_id,:name,:address,:city) RETURNING employee_id"; $statement = $this->prepare($sqlQuery); $a ="2002-03-11 12:01AM" ; $statement->bindParam(":user_id", $employee->getUserId(), PDO::PARAM_INT); $statement->bindParam(":name", $employee->getName(), PDO::PARAM_STR); $statement->bindParam(":address", $employee->getAddress(), PDO::PARAM_STR); $statement->bindParam(":city", $employee->getCity(), PDO::PARAM_STR); $statement->execute(); $result = $statement->fetch(PDO::FETCH_ASSOC); return $result["employee_id"]; } ?> ---- Server IP: 69.147.83.197 Probable Submitter: 213.84.116.84 ---- Manual Page -- http://www.php.net/manual/en/pdo.lastinsertid.php Edit -- https://master.php.net/note/edit/102614 Del: integrated -- https://master.php.net/note/delete/102614/integrated Del: useless -- https://master.php.net/note/delete/102614/useless Del: bad code -- https://master.php.net/note/delete/102614/bad+code Del: spam -- https://master.php.net/note/delete/102614/spam Del: non-english -- https://master.php.net/note/delete/102614/non-english Del: in docs -- https://master.php.net/note/delete/102614/in+docs Del: other reasons-- https://master.php.net/note/delete/102614 Reject -- https://master.php.net/note/reject/102614 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#177296) next »