php – codeigniter,result()与result_array()
发布时间:2020-12-13 21:28:15 所属栏目:PHP教程 来源:网络整理
导读:我同时使用result()和result_array(). 通常我喜欢把我的结果作为数组,这就是为什么我主要使用result_array() 但我想知道我应该遵循哪种更好的方法, 哪一个在性能方面更有效? 这是我在codeigniter查询中讨论的示例 $query = $this-db-get();$result = $query
我同时使用result()和result_array().
通常我喜欢把我的结果作为数组,这就是为什么我主要使用result_array() 但我想知道我应该遵循哪种更好的方法, 这是我在codeigniter查询中讨论的示例 $query = $this->db->get(); $result = $query->result_array(); 或者这应该是更好的方法? $query = $this->db->get(); $result = $query->result(); 现在我也在我的通用模型中使用result_array. 解决方法
Result有一个可选的$type参数,用于决定返回的结果类型.默认情况下($type =“object”),它返回一个对象(result_object()).它可以设置为“array”,然后它将返回一个结果数组,相当于caling result_array().第三个版本接受用作结果对象的自定义类.
CodeIgniter的代码: /** * Query result. Acts as a wrapper function for the following functions. * * @param string $type 'object','array' or a custom class name * @return array */ public function result($type = 'object') { if ($type === 'array') { return $this->result_array(); } elseif ($type === 'object') { return $this->result_object(); } else { return $this->custom_result_object($type); } } 数组在技术上更快,但它们不是对象.这取决于您希望在何处使用结果.大多数情况下,阵列就足够了. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |