当在类方法内部使用 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
的方法,例如mysqli或 PDO功能。
自 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/