JDillon522
JDillon522

Reputation: 19666

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

Simple problem I'm stuck on:

Here is my active record query:

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

This only returns the first row in the DB. If I get rid of ->row(); it returns this odd bit:

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
}

What's odd is that the exact same query works perfectly elsewhere in my code for different tables.

Suggestions?

Upvotes: 2

Views: 1992

Answers (2)

GautamD31
GautamD31

Reputation: 28763

Try like

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

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

row() will return only one row of the result array.

Or you can also try with result_array() as @Hashem Qolami said like

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

Upvotes: 5

Allroundstart
Allroundstart

Reputation: 3

You may also try the given code below:

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

To learn more about handling query results in CodeIgniter: Handling query results in CodeIgniter

Upvotes: 0

Related Questions