php - PHP 资源是立即释放还是应该使用 sqlsrv_free_stmt?

标签 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?

最佳答案

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

关于php - PHP 资源是立即释放还是应该使用 sqlsrv_free_stmt?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17928473/

相关文章:

php - 使用 PHP cURL 时服务器停止运行

PHP Google API - 缺少范围

php - 对禁止时间为 "datetime"的 mysql 数据库实现永久禁止。如何?

Php数组在相交键之后对关联键进行排序

php - 在 Woocommerce 中具有最大成本的基于累进数量的运输成本

php - 复杂的mysql表设计选择

php - 即使我更改参数,查询结果也是相同的

PHP:使用 header 从数据库打开网址

php - Woocommerce 获取当前的运输区域

php - 如何在 PHP 中运行后台进程并读取/写入其标准输出/标准输入?