CodeIgniter Forums
Get query results in model - Printable Version

+- CodeIgniter Forums (
+-- Forum: Archived Discussions (
+--- Forum: Archived Development & Programming (
+--- Thread: Get query results in model (/thread-58670.html)

Get query results in model - El Forum - 07-04-2013

Hello guys just want to ask about models.
I created a model called Entry_Model and I have two functions. One is to view all entries and the other one is to count. I did this by using a traditional query. I used this code:
$sql1 = "SELECT *  FROM entries";
$result1 = $this->db->query($sql1);

My problem is when i tries to get the result from my model and then passing thru the controller I got an error like this:
Fatal error: Call to a member function result_row() on a non-object in
Here's the sample code:


public function index(){

                $data['title'] = "Welcome back ".$this->session->userdata('username')." !";
                $data['viewEntries'] = $this->entry_model->viewAllEntry();
                $data['countAllEntries'] = $this->entry_model->countAllEntry();
public function viewAllEntry(){
            $data[] = array();
            $sqlViewAllEntry = "SELECT * FROM tbl_entries";
            $data['viewEntries'] = $this->db->query($sqlViewAllEntry);
            return $data;
        public function countAllEntry(){
            $data[] = array();
            $sqlCountEntries = "SELECT COUNT(fk_memid) AS all_entries FROM tbl_entries";
            $data['countAllEntries'] = $this->db->query($sqlCountEntries);
            return $data;

foreach($countAllEntries->result_row() as $row){
                $allRecord = $row['all_entries'];

echo $allRecord; //return error

But when i try not to use a model it works. Here's what i did in my view.
$sqlCountEntry = "SELECT COUNT(*) AS entry FROM tbl_entries";
            $result = $this->db->query($sqlCountEntry);
            foreach($result->result_array() as $row){
                echo $row['entry'];

What's my error? Or is there another way to perform this? By the way I used tradtional query because as of now I am confused with the use of active query. Specially when I performing a join statement. Please help me guys. Thanks.

Get query results in model - El Forum - 07-06-2013

$data['viewEntries'] = $this->db->query($sqlViewAllEntry)->result();

$data['countAllEntries'] = $this->db->query($sqlCountEntries)->row();