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