I’m using DBLIB driver for connecting to a MS SQL server 2008 database from a Linux workstation.
Since DBLIB doesn’t support PDO::lastInsertId() method I’m using SELECT SCOPE_IDENTITY() to get the last inserted id but my code isn’t working.
Let’s take a look at my code:
function setDataByQuery($query, $values){
$sth = $this->dbLink->prepare($query);
$this->bindParams($sth, $values);
$sth->execute();
var_dump($sth->fetchAll(PDO::FETCH_ASSOC));
//$this->lastId = $this->dbLink->lastInsertId();
return true;
}
private function bindParams(&$sth, $values){
$type = array('i'=>PDO::PARAM_INT, 's'=>PDO::PARAM_STR);
foreach($values as $key=>$value)
$sth->bindParam($key, $value['data'], $type[$value['type']]);
}
My method setDataByQuery is inserting without problem but when I try to get the last inserted id I get an empty array.
My insertion query looks like this:
"INSERT INTO personas (nombres, apellido_paterno, apellido_materno, fecha_nacimiento, sexo, estado_civil, direccion, codigo_postal, id_delegacion, id_empresa)
VALUES (:n,:aP,:aM,'1900-1-1','H','Unión Libre','Dirección','0000',1,1);SELECT SCOPE_IDENTITY() AS theID;";
According to this question there should be no problem, however the difference is that I’m using prepared queries and that may cause the problem. Can you help me please?
For some reason this wasn’t working when both statements where in the same query so I created another function which did the job.