How to resolve a "Catchable fatal error"

5

I'm doing some object-oriented exercises in PHP and at the time of changing the database's data, I come across the following error

  

Catchable fatal error: Object of class mysqli_result could not be converted to string in /var/www/html/Aluno/class/AlunoDAO.php on line 30

I've never seen him before (in my long career for a few months). Can anyone tell me what it means?

Follow my code

Change-data.php

$id = $_POST['id'];
$nome = $_POST['nome'];
$cpf = $_POST['cpf'];
$senha = $_POST['senha'];

$alunoDAO = new AlunoDAO();

$nome_imagem = $alunoDAO->buscaFoto($conexao, $id);

$aluno = new Aluno($nome, $cpf, $senha, $nome_imagem);

$alunoDAO->alteraDados($conexao, $aluno, $id);

and my function in class AlunoDAO (the one being pointed to an error).

function alteraDados($conexao, $aluno, $id){
        $senhaMD5 = md5($aluno->getSenha());
        $query = "update alunos set nome = '{$aluno->getNome()}', cpf = '{$aluno->getCpf()}', senha = '{$senhaMD5}', imagem = '{$aluno->getNomeImagem()}' where id = {$id}";
        $resultado = mysqli_query($conexao, $query);
        return $resultado;
    }
    
asked by anonymous 04.10.2015 / 03:31

1 answer

5

To avoid errors of type Catchable error , you must catch the thrown exception, for more detailed and controlled error information, exceptions sometimes even making some sensitive data available.

The solution is to instantiate this function, and call it on the page where the errors are returned, in your case it will be on the page where you create an instance of the classes.

function myErrorHandler($errno, $errstr, $errfile, $errline) {
    if ( E_RECOVERABLE_ERROR===$errno ) {

        echo "'Catched:' catchable fatal error (". $errstr .")\n";

        return true;    
    }
    return false;
}

set_error_handler('myErrorHandler');

To fix the error, in your function, simply do the fetch , before trying to return the value:

function buscaFoto($conexao, $id){
        $query = "select imagem from alunos where id = {$id}";
        $resultado = mysqli_query($conexao, $query);
        if($resultado){
            $linha = mysqli_fetch_assoc($resultado);
            return $linha['imagem'];    
        } else {
            return false;   
        }
    }

For more information read:

Set_Error_Handler - PHP.net

Catching Catchable Errors - SOen

    
04.10.2015 / 05:10