CodeIgniter Forums
Fatal error: Call to a member function ... on a non-object - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Fatal error: Call to a member function ... on a non-object (/showthread.php?tid=18374)

Pages: 1 2


Fatal error: Call to a member function ... on a non-object - El Forum - 05-04-2009

[eluser]philstan[/eluser]
I love Codeigniter...However, have wasted a couple of hours trying to find the solution to this problem.

I am getting this error on a view page:

Fatal error: Call to a member function get_user_name() on a non-object


while running this in a view file (similar code has run successfully in a controller, however design needs mandate its use here in the view):

//Search for user_name
$this->load->model('Company_info');

$get_name = $row->author;

$this->Company_info->get_user_name($get_name);

I have tried loading the model with 'company_info', but still no success. The database is loaded automatically in the config file.

Thank you in advance for your help.


Fatal error: Call to a member function ... on a non-object - El Forum - 05-04-2009

[eluser]abmcr[/eluser]
[quote author="philstan" date="1241483152"]

//Search for user_name
$this->load->model('Company_info');

$get_name = $row->author;

$this->Company_info->get_user_name($get_name);

[/quote]

Are you sure to use a
$query=$this->db->query("YOUR SQL");
$row=$query->row();

or you use a
$row=$query->row_array();
i.e. a row_array function?
What about the get_user_name function?


Fatal error: Call to a member function ... on a non-object - El Forum - 05-04-2009

[eluser]philstan[/eluser]
Thank you abmcr for your response.

I should have included this in my original post. This is the relevant code in the model:

function get_user_name($user_id)
{
$query = $this->db->query("SELECT * FROM users WHERE user_id = ".$user_id."");
$row = $query->row();
return $row->user_name;
}

This is not-functioning as it is here.

Thanks Philstan


Fatal error: Call to a member function ... on a non-object - El Forum - 05-04-2009

[eluser]TheFuzzy0ne[/eluser]
Ideally, you should always check for a result, and perhaps return FALSE if there isn't one.

Code:
function get_user_name($user_id)
{
    $query = $this->db->query("SELECT * FROM users WHERE user_id = ?", $user_id);
    if ($query->num_rows() > 0)
    {
        $row = $query->row();
        return $row->user_name;
    }
    return FALSE;
}



Fatal error: Call to a member function ... on a non-object - El Forum - 05-04-2009

[eluser]Thorpe Obazee[/eluser]
Yeah, much better to that so that you can check the return value and probably do something.


Fatal error: Call to a member function ... on a non-object - El Forum - 05-04-2009

[eluser]philstan[/eluser]
Thank you all for the very useful replies, however they do not address my original post. My original problem is still that I'm getting this error message:

Fatal error: Call to a member function get_user_name() on a non-object

The function which calls the model is not being recognized, so no SQL is being processed at all. I don't understand why this is happening as I have used this code in another view with no problems.


Fatal error: Call to a member function ... on a non-object - El Forum - 05-04-2009

[eluser]Thorpe Obazee[/eluser]
[quote author="philstan" date="1241513715"]Thank you all for the very useful replies, however they do not address my original post. My original problem is still that I'm getting this error message:

Fatal error: Call to a member function get_user_name() on a non-object

The function which calls the model is not being recognized, so no SQL is being processed at all. I don't understand why this is happening as I have used this code in another view with no problems.[/quote]

Code:
$this->Company_info->get_user_name($get_name);

Change that to:

Code:
$this->company_info->get_user_name($get_name);



Fatal error: Call to a member function ... on a non-object - El Forum - 05-05-2009

[eluser]philstan[/eluser]
Please note in my original post that I have tried

$this->company_info->get_user_name($get_name);

However this did not and still does not work.

Thank you


Fatal error: Call to a member function ... on a non-object - El Forum - 05-05-2009

[eluser]Thorpe Obazee[/eluser]
[quote author="philstan" date="1241586255"]Please note in my original post that I have tried

$this->company_info->get_user_name($get_name);

However this did not and still does not work.

Thank you[/quote]

I assumed that you didn't use it since I can't see it in the first post. Noted.

On which line did the error point?


Fatal error: Call to a member function ... on a non-object - El Forum - 05-05-2009

[eluser]Gordaen[/eluser]
If you do print_r($this->Company_info); above your line that reads $this->Company_info->get_user_name($get_name);, what do you get?