note 102614 modified in pdo.lastinsertid by danbrown
| From: | danbrown@php.net | Date: | Thu, 24 Feb 2011 15:57:19 +0000 |
| Subject: | note 102614 modified in pdo.lastinsertid by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-177298@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.
<?php
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"];
}
?>
--was--
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"];
}
?>
http://php.net/manual/en/pdo.lastinsertid.php