Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
这里是立即释放PHP资源还是应该使用sqlsrv_free_stmt?_Php - Fatal编程技术网

这里是立即释放PHP资源还是应该使用sqlsrv_free_stmt?

这里是立即释放PHP资源还是应该使用sqlsrv_free_stmt?,php,Php,如果没有从sqlsrv\u查询返回任何行,或者在遍历资源的所有行之后,是否立即释放PHP资源 例如,下面是sqlsrv\u free\u stmt($theRS)语句实际执行任何操作,或者资源是否已自动释放 $theRS = sqlsrv_query(...xxx...) if (!sqlsrv_has_rows($theRS)) { echo('No match'); } else { while($theARR = sqlsrv_fetch_array($theRS)) {

如果没有从
sqlsrv\u查询返回任何行,或者在遍历资源的所有行之后,是否立即释放PHP资源

例如,下面是
sqlsrv\u free\u 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?

PHP在完成对资源的迭代后不会立即释放资源。当
sqlsrv\u查询
没有返回任何结果时,它也不会立即释放资源

例如,您可以使用这些代码,看看会发生什么。即使没有结果仍然有一个资源

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

第一组回声将在箭头之间显示资源-->此处显示资源'$“其他的。”$塞尔斯。”如果没有更多行可用,php将不会自动释放,因为php不知道您是否要执行
倒带
搜索
-类型调用以跳回结果集中。一般来说,当事情超出范围时,php会为您清理,当脚本退出时,php肯定会清理。除非你在写一个长时间运行的脚本,否则你通常没有什么理由去清理你自己。@MarcB这会是一个很好的答案。:)