0

Array to string conversion is thrown when I try to get a specific id and don't let me save the form. i just need the id of 'intervaloHorario' in order to autofill a field,but i don't know what am i doing wrong. Please give me a hand.

Error

Severity: 4096

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

Model function

    public function get_idintervalo($idCitas) {

            $query = $this->db->query('SELECT intervaloshorarios.idIntervaloHorario FROM intervaloshorarios INNER JOIN citas '
                    . 'ON intervaloshorarios.idIntervaloHorario = citas.idIntervaloHorario '
                    . 'WHERE citas.idCitas = ' . $idCitas . ';');

            return $query->result();
}

Controller

$query = $this->Intervalos_Model->get_idintervalo($idCitas);


        if ($crud->getState() == "add") {
//It's wroking fine getting this
            $crud->change_field_type('idCitas', 'hidden', $idCitas);

foreach ($query as $key) {
            //Is not working fine (throws array to string conversion)
            $crud->change_field_type('idIntervaloHorario', 'hidden', $key);
        }
1
  • I think you can do a gettype($query) to see the type of $query variable. Commented May 24, 2017 at 19:08

2 Answers 2

1

Assuming you are using CodeIgnitor Framework. You should write

foreach ($query as $key) {
    //Is not working fine (throws array to string conversion)
    $crud->change_field_type('idIntervaloHorario', 'hidden', $key->idIntervaloHorario);
}

because $query stores the query resultset in objects and $key is an object here. For more see manual https://www.codeigniter.com/userguide2/database/results.html

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much!! I am new with this and i've been looking for days a working answer!! you're a crack!
0

Is this OpenCart? If so use:

return $query->rows;

to get an array of returned rows.

1 Comment

Thank you for your answer! i got what i was looking for thanks to @simurgrai

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.