是否立即释放了PHP资源,还是应该使用sqlsrv_free_stmt?

时间:2013-07-29 15:48:40

标签: php

如果没有从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?

1 个答案:

答案 0 :(得分:2)

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

例如,您可以使用此代码并查看会发生什么。即使没有 结果 ,仍然有资源。

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

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

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