0

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
5
  • could you please give a bit more of code, looks like there is some issue with object making and useage Commented Apr 16, 2014 at 5:21
  • @Arihant what about now? Commented Apr 16, 2014 at 5:24
  • Did you loaded the database in constructor ??? $this->load->database(); Commented Apr 16, 2014 at 5:25
  • you can see it that i did ;) Commented Apr 16, 2014 at 5:27
  • These fields are not selected with their related table names fa_shrt_name, en_shrt_name, fa_text, en_text, grp_id, image. They may be ambigous Commented Apr 16, 2014 at 5:30

5 Answers 5

1

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();
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

Change:

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

instead

return $query->result_array();

1 Comment

i used $this->db->query() so i don't think that's gonna work here's the result by the way : Call to a member function get() on a non-object
0

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

Comments

0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.