繁体   English   中英

PDO DBLIB范围标识不起作用

[英]PDO DBLIB Scope Identity not working

我正在使用DBLIB驱动程序从Linux工作站连接到MS SQL Server 2008数据库。

由于DBLIB不支持PDO :: lastInsertId()方法,因此我正在使用SELECT SCOPE_IDENTITY()获取最后插入的ID,但是我的代码无法正常工作。

让我们看一下我的代码:

  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']]);
    }

我的方法setDataByQuery插入没有问题,但是当我尝试获取最后插入的ID时,我得到一个空数组。

我的插入查询如下所示:

"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;";

根据这个问题应该没有问题,但是区别在于我使用的是准备好的查询,这可能会引起问题。 你能帮我吗?

由于某些原因,当两个语句都在同一查询中时,这是行不通的,因此我创建了另一个完成此功能的函数。

function setDataByQuery($query, $values){
    $sth = $this->dbLink->prepare($query);
    $this->bindParams($sth, $values);
    if(!$sth->execute())
            return false;
    $this->lastId = $this->lastInsertId();
    //$this->lastId = $this->dbLink->lastInsertId();
    return true;
    }
 private function lastInsertId($seqname = null){
    $sth = $this->dbLink->prepare('SELECT SCOPE_IDENTITY() AS id');
    $sth->execute();
    $result = $sth->fetch();
    return $result['id'];
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM