查询"SELECT SCOPE_IDENTITY()AS ID"时,用于PHP的Microsoft sqlsrv驱动程序不返回任何结果. [英] Microsoft´s sqlsrv driver for PHP not returning any result when querying "SELECT SCOPE_IDENTITY() AS id"

查看:112
本文介绍了查询"SELECT SCOPE_IDENTITY()AS ID"时,用于PHP的Microsoft sqlsrv驱动程序不返回任何结果.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用php_mssql驱动程序,此查询工作正常:

this query works fine using the php_mssql driver:

INSERT INTO Table(columnName) VALUES ('text');
SELECT SCOPE_IDENTITY() AS id;

表确实有一个id列,它是一个标识. 我将执行该查询,并获取表中的最后一个ID.

Table does have an id column, which is an identity. I would execute that query, and get the last id in the table.

如果使用Microsoft的php_sqlsrv驱动程序执行查询,则相同的代码将不起作用.

The same code doesn´t work if the query is executed using Microsoft´s php_sqlsrv driver.

执行查询(sqlsrv_query函数)时我没有收到任何错误,但是调用sqlsrv_fetch_array时出现以下错误: 查询的活动结果不包含任何字段"

I don´t get any error when executing the query (sqlsrv_query function) , but i get the following error when calling sqlsrv_fetch_array: "The active result for the query contains no fields"

我在Google上搜索了很多,却没有找到答案,这让我感到非常惊讶,以前没有人遇到过这个问题,尽管官方"驱动程序似乎没有人使用此驱动程序自PHP 5.3发行以来...

I´ve googled a lot, and didn´t find no answer, it was a big surprise for me that no one faced this problem before, it seems like nobody is using this driver, even though is the "official" one since PHP 5.3 release...

谢谢.

推荐答案

在最初的CTP中,字段索引从1开始.后来,它们被更改为从0开始.

In the initial CTP the field indices started at 1. Later they were changed to start at 0.

尝试这样的事情:

// connection to the dbserver

$result = sqlsrv_query("INSERT INTO Table(columnName) VALUES ('text'); SELECT SCOPE_IDENTITY() AS ID");

echo "The last insert_id is".lastId($result);

function lastId($queryID) {
     sqlsrv_next_result($queryID);
     sqlsrv_fetch($queryID);
     return sqlsrv_get_field($queryID, 0);
}

这篇关于查询"SELECT SCOPE_IDENTITY()AS ID"时,用于PHP的Microsoft sqlsrv驱动程序不返回任何结果.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆