首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在php调用中获取MS存储的proc的输出(sqlsrv)

在php调用中获取MS存储的proc的输出(sqlsrv)
EN

Stack Overflow用户
提问于 2011-12-02 19:21:06
回答 2查看 9.1K关注 0票数 0

我正在使用php的sqlsrv ms驱动程序,它工作得很好(用正常的查询进行了测试),我还测试了它运行一个存储过程来更新表数据的工作,我知道我想用它来运行一个存储过程,我想要得到响应,怎么做呢?

代码语言:javascript
运行
复制
$server = "...the server address...";
$options = array("UID"=>"...the username...","PWD"=>"...the password...",
  "Database" => "...the database..."
  );

$conn = sqlsrv_connect($server, $options);

if ($conn === false) {die("<pre>".print_r(sqlsrv_errors(), true));}

$tsql_callSP = "{call ...the stored proc...( ?, ?)}";

$params = array( 
                 array("...first value in...", SQLSRV_PARAM_IN),
                 array("...second value in...", SQLSRV_PARAM_IN)
               );

$stmt3 = sqlsrv_query( $conn, $tsql_callSP, $params);
if( $stmt3 === false )
{
     echo "Error in executing statement 3.\n";
     die( print_r( sqlsrv_errors(), true));
}

print_r( $stmt3); //attempting to print the return but all i get is Resource id #3
echo "test echo";

sqlsrv_free_stmt( $stmt3);
sqlsrv_close( $conn); 

我知道我可以使用输出参数,但我总是会从存储的proc中收到多个值。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-12-02 19:28:04

假设存储过程用一条SELECT语句返回一个表的内容,使用它的输出应该和使用sqlsrv_query的结果一样简单,就像使用任何其他选择查询结果一样(即在结果上使用sqlsrv_query/array)!因此,存储的proc可能如下所示:

代码语言:javascript
运行
复制
CREATE STORED PROCEDURE test
AS
    -- do some other stuff here
    -- ...
    SELECT * FROM test
GO

在你的php中:

代码语言:javascript
运行
复制
// establish db connection, initialize
// ...

$sql = "{call test}"
$result = sqlsrv_query($conn, $sql);
while (sqlsrv_fetch_object($result))
{
     // do something with the data
     // ...
}
票数 3
EN

Stack Overflow用户

发布于 2011-12-02 19:28:02

需要调用sqlsrv_fetch()sqlsrv_get_field()从返回的语句中获取数据。

From the example code in the manual for sqlsrv_get_field

代码语言:javascript
运行
复制
$stmt = sqlsrv_query( $conn, $tsql);
if( $stmt === false )
{
     echo "Error in statement preparation/execution.\n";
     die( print_r( sqlsrv_errors(), true));
}

/* Make the first row of the result set available for reading. */
if( sqlsrv_fetch( $stmt ) === false )
{
     echo "Error in retrieving row.\n";
     die( print_r( sqlsrv_errors(), true));
}

/* Note: Fields must be accessed in order.
Get the first field of the row. Note that no return type is
specified. Data will be returned as a string, the default for
a field of type nvarchar.*/
$name = sqlsrv_get_field( $stmt, 0);
echo "$name: ";

除此之外,我不确定当您说将收到多个值时,您的意思是一行中将有多个字段(在这种情况下,您将希望对sqlsrv_get_field()进行更多调用)、多行(在这种情况下,您必须使用while循环并在循环中调用sqlsrv_fetch() ),或者是多个结果集(在这种情况下,您将需要使用sqlsrv_next_result()的while循环)。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8355577

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档