note 102614 modified in pdo.lastinsertid by danbrown

[email protected] Thu, 24 Feb 2011 07:57:19 -0800
Newsgroups php.notes
Message-ID <[email protected]>
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