CodeIgniter活动记录查询仅返回表中的第一个条目

时间:2013-08-27 05:27:39

标签: php codeigniter activerecord

简单问题我坚持:

这是我的有效记录查询:

public function get_org()
  {
    return $this->db
          ->get('organizations')
          ->row();
  }

这只返回数据库中的第一行。 如果我摆脱->row();,它会返回这个奇数位:

object(CI_DB_mysql_result)#17 (8) {
  ["conn_id"]=>
  resource(8) of type (mysql link persistent)
  ["result_id"]=>
  resource(12) of type (mysql result)
  ["result_array"]=>
  array(0) {
  }
  ["result_object"]=>
  array(0) {
  }    
  ["custom_result_object"]=>
  array(0) {
  }
  ["current_row"]=>
  int(0)
  ["num_rows"]=>
  int(2)
  ["row_data"]=>
  NULL
}

奇怪的是,完全相同的查询在我的代码中的其他地方完美地工作。

建议?

2 个答案:

答案 0 :(得分:5)

尝试

public function get_org()
{
   $query = $this->db->get('organizations');

   foreach ($query->result() as $row)
   {
      $result_arr[] = $row;
   }
   return $result_arr; 
}

row()将只返回结果数组的一行。

或者您也可以尝试result_array() @Hashem Qolami 表示喜欢

public function get_org()
{
   $result = $this->db->get('organizations');
   return $result->result_array();
}

答案 1 :(得分:0)

您也可以尝试下面给出的代码:

$query = $this->db->get('organizations');
return $query->result_array();

要详细了解如何在CodeIgniter中处理查询结果:Handling query results in CodeIgniter