php - 当局部变量不再存在时,mysql_query 是否释放内存?

标签 php mysql memory-management

当在类方法内部使用 mysql_query 并将其设置为局部变量时,内存是在方法完成执行时还是在脚本结束时释放的?是否值得添加 mysql_free_result?下面是示例。

class example{
    public function Query($query){
        $result = mysql_query($query);
        while ($row = mysql_fetch_assoc($result))
        {
            $this->rows[] =$row;
        }
    }
}

方法结束时会释放内存还是需要在方法结束时调用mysql_free_result

最佳答案

首先,mysql_ php 函数不再维护,因此您可能希望使用更OOP 的方法,例如mysqliPDO功能。

mysql_free_result()

will free all memory associated with the result identifier result.

如果您正在获取大量结果,那么是的,您可以(并且应该)使用此功能来释放您的 mysql 以节省内存。

至于你的问题的其余部分:

Will memory be freed when the method finishes or is it necessary to call mysql_free_result at the end of the method?

不,如果你没有使用 mysql_free_result,内存不会在你的方法结束时释放,而是在你的脚本执行结束时释放。

希望对您有所帮助。干杯。

关于php - 当局部变量不再存在时,mysql_query 是否释放内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13328235/

相关文章:

php - 过滤时长

php - 如何在PhpSpreadsheet中对齐单元格值?

mysql - EF6 MySQL 更改所有表前缀

iphone - iPhone 内存管理的最佳解释在哪里?

php - (Laravel) 如何在 1 个路由中使用 2 个 Controller ?

php - 消息数组到字符串的转换

MySQL,如何按降序显示 child 的年龄以及按字母顺序列出的同龄 child ?

c++ - 检测堆损坏

c - 内存分配问题?

PHP POST 打印复选框条目 : nothing I have found on forums works