hamid reza
hamid reza

Reputation: 45

CodeIgniter: Function call result_array() gives error

I'm getting this error for like a hour, what's this error on codeigniter

Here is my model:

i described property for fields too

class news_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();  
        $this->load->database();
    }

    public function get_news($id = false)
    {
        if ($id === false) {
            $query = $this->db->query("SELECT tbl_news.id,
                                            tbl_news.fa_name,
                                            tbl_news.en_name,
                                            tbl_news.fa_shrt_name,
                                            tbl_news.en_shrt_name,
                                            tbl_news.fa_text,
                                            tbl_news.en_text,
                                            tbl_news.image,
                                            tbl_news.grp_id,
                                            tbl_news_grp.fa_name FROM tbl_news JOIN tbl_news_grp ON tbl_news_grp.id = tbl_news.id ");

            return $query->result_array();
        }

        $query = $this->db->get_where('tbl_news',array('id' => $id));
        return $query->result_array();
    }
}

I get this error :

Fatal error: Call to a member function result_array() on a non-object in 
C:\xampp\htdocs\ipkoroosh\application\models\news_model.php on line 19

Upvotes: 0

Views: 4657

Answers (5)

Rakesh Sharma
Rakesh Sharma

Reputation: 13728

How your where condition work you have no query above this and both condition will not run at a time so need to use else and refine query according you

public function get_news($id = false)
    {
        if ($id === false) {
            $query = $this->db->query("SELECT tblnews.*,tblnewsgrp.* FROM tbl_news tblnews JOIN tbl_news_grp tblnewsgrp ON tblnewsgrp.id = tblnews.id");

            return $query->result_array();
        }
        else {
           $query = $this->db->query("SELECT * FROM tbl_news WHERE id = '$id'");
            return $query->result_array();
        }
    }

Upvotes: 1

Parimal
Parimal

Reputation: 166

Check your query properly i think it is not giving result so this error is there.

Check it with this. $query->result();

Upvotes: 0

Pankaj Garg
Pankaj Garg

Reputation: 1322

Try to print your SQL query by using $this->db->last_query(); and run the query on mysql console. There may be errors in SQL query.

Please check

Upvotes: 0

Pratik Butani
Pratik Butani

Reputation: 62411

Change:

return $query->get()->result_array();

instead

return $query->result_array();

Upvotes: 0

Arihant
Arihant

Reputation: 4047

Result has an optional $type parameter which decides what type of result is returned. By default ($type = "object"), it returns an object (result_object()). It can be set to "array", then it will return an array of result, that being equivalent of caling result_array(). The third version accepts a custom class to use as a result object.

/**
* Query result. Acts as a wrapper function for the following functions.
*
* @param string $type 'object', 'array' or a custom class name
* @return array
*/
public function result($type = 'object')
{
if ($type === 'array')
{
    return $this->result_array();
}
elseif ($type === 'object')
{
    return $this->result_object();
}
else
{
    return $this->custom_result_object($type);
}
}

Reference: Stackoverflow Answer

Upvotes: 0

Related Questions