php - free_result() 与 MY_Model

标签 php mysql codeigniter

我在尝试创建的 MY_Model 中有一个 get 函数,它返回单个记录或所有记录。我想做的是,当我得到结果时,总是用 free_result() 释放一些内存,但如果我把它放在返回结果的行中,我什么也得不到。请问有什么建议吗?

public function get($id = NULL, $single = FALSE){

        if ($id != NULL) {

            $filter = $this->_primary_filter; // filter the id
            $id = $filter($id); // e.g. intval($id)

            $this->db->where($this->_primary_key, $id);

            $method = 'row'; // single record
        } elseif ($single === TRUE) {
            $method = 'row'; // single record
        } else {
            $method = 'result'; // all records
        }
        return $this->db->get($this->_table_name)->$method();
    }

最佳答案

在 Controller 中使用结果之前,无法释放结果内存。结果可能不是通过值返回,而是通过指针/引用返回,因此结果始终位于内存中的同一位置,直到呈现页面为止。

使用free_result(),您可以在 Controller 使用结果之前将其销毁。

顺便说一句:结果也可能作为指针/引用发送到 View ,因此如果在渲染页面之前销毁 Controller 中的结果,则可能会出现没有结果的页面。

关于php - free_result() 与 MY_Model,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17719043/

相关文章:

mysql - 使用子查询计算表一和表二的联合中的不同值

php - WordPress:获取数据并排序

php - MySQL 在 PHP 中按错误排序

php - 在 Codeigniter 中编写模型代码的最佳方式是什么

php - CodeIgniter 表单提交重定向到本地主机

php - 在 php 中执行我自己的 groovy 脚本

php - 带有 PHP post 方法的 HTML5 表单

php - 字符显示正常,然后插入 mysql 时带有问号

php - 如何将 JSON 数组添加到 PHP 中的现有 JSON 中?

php - 我的 php 脚本恰好在 120 秒后停止