简体   繁体   中英

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
}

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

Suggestions?

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();
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM