Problems with mysqli_result as array

Asked

Viewed 1,429 times

2

I’m trying to make the page display the documents sent by the user, but always returns me the same error

Fatal error: Cannot use Object of type mysqli_result as array in >C: wamp64 www Project-School Student-Query.php on line 158

The code on the line is this

<h1>Documentos:</h1>
<?php $arrayDadosDocumentos = ObtemDocumentos($rg);
$idTipo = $arrayDadosDocumentos[2];?>

I have researched and always find the same solution, talking to use ``, but I do not know how to apply in my code.

At the moment that’s it, thank you for your attention.

p.s.: If you need any more information, please let me know.

Code of ObtemDocumento()

function ObtemDocumentos($rg){
    include("conexao.php");

    $SQL ="SELECT D.* FROM documento D JOIN alunodocumento AD WHERE AD.RG = ".$rg." AND D.IdCaixa = AD.IdCaixa AND D.IdLote = AD.IdLote AND D.idTipoDoc = AD.idTipoDoc ORDER BY D.idTipoDoc;" ;

    $resultado = mysqli_query($conexao, $SQL);

    if (!$resultado) {
        $mensagem_erro  = 'Erro de sintaxe na query: ' . mysqli_error() . "<br>";
        $mensagem_erro .= 'SQL executado: ' . $SQL;
        $destino = 'http://localhost/Projeto-escolweb/php/erro.php?msg='.$mensagem_erro;
        header("Location: $destino");
        exit;
    }

   return $resultado;
  • The problem is in ObtemDocumentos() probably missing the while with the mysqli_fetch_assoc().

  • 1

    could put the code of ObtemDocumentos?

  • I edited the question, code is at the end of it.

  • Guy the code I posted works perfectly in your case.

2 answers

2


It may be more practical to make the function return the array with the data, then add the call from mysqli_fetch_assoc() while and at the end of it returns the variable ($arr) containing the details of the consultation:

$resultado = mysqli_query($conexao, $SQL);
if (!$resultado) {
    $mensagem_erro  = 'Erro de sintaxe na query: ' . mysqli_error() . "<br>";
    $mensagem_erro .= 'SQL executado: ' . $SQL;
    $destino = 'http://localhost/Projeto-escolweb/php/erro.php?msg='.$mensagem_erro;
    header("Location: $destino");
    exit;
}

$arr = array();
while($row = mysqli_fetch_assoc($resultado)){
    $arr[] = $row;
}

return $arr;
  • Really, that works better

1

$query = ObtemDocumentos($rg);
while($dados = mysqli_fetch_array($query)){
    $idTipo = dados["idTipo"];
}
//se eu ajudei por favor vote na resposta
  • I don’t know if that fact didn’t work, or if I implemented your spouse the wrong way

  • The data that ta inside the while is fictitious, just give a print_r in the $data variable and you will see that it takes the data one by one.

  • I’ve always used it this way

  • this was the return of the print_r >Array ( [0] => Array ( [Idbox] => 1 [Idlot] => a [idTipoDoc] => 1 >[Path] => uploads/01-a-9306088604-1-tmp [Datainclusion] => >2016-10-13 08:27:09 ) [1] => Array ( [Idcaixa] => 1 [Idlote] => b >[idTipoDoc] => 2 [Caminho] => uploads/01-b-9306088604-2-tmp [Datainclusao] => 2016-10-13 08:32:00 ) )

  • And weren’t these the data you were needing ? kkkk Use the other guy’s answer, it’ll be easier when it comes to servicing.

  • Really, are the data I need, however the return is all messed up, I will have to stir a little kkkkkkk thank you very much for the help!

Show 1 more comment

Browser other questions tagged

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