2

如果没有从 a 返回的行sqlsrv_query或在遍历资源的所有行之后,是否会立即释放 PHP 资源?

例如,下面的sqlsrv_free_stmt($theRS);语句实际上是在做任何事情,还是资源已经自动释放?

$theRS = sqlsrv_query(...xxx...)

if (!sqlsrv_has_rows($theRS)) {
    echo('No match');
}
else {
    while($theARR = sqlsrv_fetch_array($theRS)) {
        //code here
    }
}

sqlsrv_free_stmt($theRS);   //Does this do anything or is the resource already freed at this point?
4

1 回答 1

2

PHP 在完成对资源的迭代时不会立即释放资源。sqlsrv_query当不返回任何结果时,它也不会立即释放资源。

例如,您可以玩弄这段代码,看看会发生什么。即使没有结果,仍然有资源。

第一组回显将在箭头之间显示资源 --> 资源显示在此处<---。它还说这是一种资源。

释放资源后的第二组回显显示 ---><--- 这不是资源。

$theQUERY = "SELECT * FROM theTable 
    WHERE ID = '1' AND ID <> '1'"   //make sure it doesn't return anything
$theRS = sqlsrv_query($conn, $theQUERY)

echo('1 - before freeing theRS = -->' . $theRS . '<--<br>');

if (is_resource($theRS)) 
{
    echo('1 - this is a resource <br>');
}
else {
    echo('1 - this is not a resource <br>');
}

echo('<br>');
sqlsrv_free_stmt($theRS);

echo('2 - after freeing theRS =  -->' . $theRS . '<--<br>');

if (is_resource($theRS)) 
{
    echo('2 - this is a resource <br>');
}
else 
{
    echo('2 - this is not a resource <br>');
}
于 2013-07-30T03:30:25.573 回答