简体   繁体   中英

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

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

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

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

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

Change:

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

instead

return $query->result_array();

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

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