Welcome Guest, Not a member yet? Register   Sign In
Fatal error: Call to a member function ... on a non-object
#1

[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.
#2

[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?
#3

[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
#4

[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;
}
#5

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

[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.
#7

[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);
#8

[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
#9

[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?
#10

[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?




Theme © iAndrew 2016 - Forum software by © MyBB