简体   繁体   English

无法从PHP函数返回MSSQL查询结果

[英]Trouble returning MSSQL query results from PHP function

I'm getting a strange error when attempting to return the results of a working sqlsrv_query function call. 尝试返回有效的sqlsrv_query函数调用的结果时出现一个奇怪的错误。

I have set up: 我已经设置:

function test($someinput, &$someoutput)
{
  //set up $dbc and $dbcOptions
  //generate tsql
  $params = array();
  $stmt = sqlsrv_query($dbc, $tsql, $params, $dbcOptions);
  $someoutput = $stmt;
  return $stmt;
}

$outputvar;
$results = test($inputvar, $outputvar);

//---ERROR being thrown here
$rows = sqlsrv_num_rows($results);
//$rows2 = sqlsrv_num_rows($outputvar);
//---ERROR being thrown here

In both lines (doesn't matter which I use), I'm getting an error in my php-errors.log file saying 在两行中(我使用哪一个都没关系),我的php-errors.log文件中出现错误,说

PHP Warning:  sqlsrv_num_rows(): 2 is not a valid ss_sqlsrv_stmt resource in
E:\inetpub\wwwroot\directory\searchtest.php on line 58

which searchtest.php is the file I'm working on and line 58 is where I have the error-producing code. 哪个searchtest.php是我正在处理的文件,第58行是我产生错误的代码的位置。

Why can I not pass query results from a function like this? 为什么我不能通过这样的函数传递查询结果?

As per OP's wish to mark as answered, comment ( slightly modified ) to answer: 按照OP希望标记为已回答的要求,请评论( 稍作修改 )以回答:

This sounds be a scope issue. 这听起来是一个范围问题。

Try passing $dbc to your function ( best ), or ( not recommended ) to make it global $dbc; 尝试将$dbc传递给函数( 最佳 ),或者( 不推荐 )传递给global $dbc; in your function. 在你的职能。

It looks to me, that you have an error in your SQL statment. 在我看来,您的SQL陈述式有误。 You can call sqlsrv_errors after: 您可以在以下时间调用sqlsrv_errors

$stmt = sqlsrv_query($dbc, $tsql, $params, $dbcOptions);

if( $stmt === false ) {
    if( ($errors = sqlsrv_errors() ) != null) {
        foreach( $errors as $error ) {
            echo "SQLSTATE: ".$error[ 'SQLSTATE']."<br />";
            echo "code: ".$error[ 'code']."<br />";
            echo "message: ".$error[ 'message']."<br />";
        }
    }
}

Then you should find the error. 然后,您应该找到错误。

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

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