Error Object of class stdClass could not be converted to string in codeigniter

Asked

Viewed 704 times

0

I’m not getting out of place because of this mistake.

My model looks like this

public function conf($dados){
    $this->db->select('id');
    $this->db->where('senha',$dados);
    $query = $this->db->get('email');
    return $query->row();
}

and my controller is as follows:

public function fim(){
    $this->load->model('quest_model');
    $this->load->helper('quest');
    $data = $this->input->post('senha');
    $dados['quest'] = $this->email_model->emailconf($data);
    $dados['quest1'] = $this->quest_model->get_quest(); 
    $dados['quest2'] = $this->quest_model->get_quest2(); 
    $dados['ques1'] = $this->quest_model->get_ques(); 
    $dados['ques2'] = $this->quest_model->get_ques2(); 
    $this->load->view('quest/file',$dados);
}

I already changed the $data for $data['p'] for example and did not stop giving this error the same way.

the error you are making is as follows: PHP Error was encountered

Severity: 4096

Message: Object of class stdClass could not be converted to string

Filename: quest/file.php

Line Number: 49

I used the print_r and is printing everything right, I’m not able to find the error, if you can help me thank you very much

thank you all

  • Not knowing what’s in that method emaiconf(), what you can suggest is debugging by creating a breakpoint. On the line before $dados['quest'] , put it like this: var_dump($data); and on the next line put var_dump($this->email_model->emailconf($data)); exit;. Put the question there, what comes back. Just be careful with sensitive data, if you have a password and things like that, replace it with dummy data before you go public.

  • Enter all the code including the View. The file.php also.

  • hello daniel, I did what you said, what you returned was: string(9) "senhaX" object(stdClass)#20 (1) { ["idques"]=> string(1) "5" }

1 answer

1

It’s unclear where that 49 line is where the error occurs, but I’m guessing it’s on view. The view must be doing something like echo $dados['quest']; and, as $dados['quest'] is a stdClass, this error message is displayed.

Therefore, I believe it is enough to do so:

$dados['quest'] = $this->email_model->emailconf($data)->idques;

Remove the breakpoint and test in a way that will probably cure the problem.

But be aware that I cannot guarantee that the return will always be one stdClass and in that object will always have the property idques. In this case the consistency of the return is your responsibility to verify.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.