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


CodeIgniter active record query only returning the first entry in the table

我遇到的简单问题:

这是我的活动记录查询:

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

这只返回DB中的第一行。如果我去掉->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
}

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

建议?

像一样尝试

public function get_org()
{
   $query = $this->db->get('organizations');
   foreach ($query->result() as $row)
   {
      $result_arr[] = $row;
   }
   return $result_arr; 
}

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

或者你也可以像一样使用result_array()

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

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

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

要了解有关在CodeIgniter中处理查询结果的更多信息:在CodeIgnater 中处理查询查询结果