Error: Catchable fatal error: Object of class stdClass could not be converted to string in

Asked

Viewed 913 times

0

I am trying to make an html table equal to my database table, however, on the page that the table appears the following error:

Catchable fatal error: Object of class stdClass could not be converted to string in

The part of the code responsible for the error is:

<?php
//Conexão e consulta ao Mysql
$con = mysqli_connect('localhost', 'root', '');
mysqli_select_db($con, 'sis_tam');
$qry = mysqli_query($con, "select * from clientes");

//Pegando os nomes dos campos
$num_fields = mysqli_num_fields($qry);//Obtém o número de campos do resultado

for($i = 0;$i<$num_fields; $i++){//Pega o nome dos campos
    $fields[] = mysqli_fetch_field_direct($qry, $i);
}

//Montando o cabeçalho da tabela
$table = '<table class="table table-hover table-inverse" style="margin-top:50;background-color: #37444a; color:lightgrey;"> <tr>';

for($i = 0;$i < $num_fields; $i++){
    $table .= '<th>'.$fields[$i].'</th>'; //ESSA É A LINHA DO ERRO
}

//Montando o corpo da tabela
$table .= '<tbody style="
    background-color: #86979e;
    color: #37444a;    
">';
while($r = mysqli_fetch_array($qry)){
    $table .= '<tr>';
    for($i = 0;$i < $num_fields; $i++){
        $table .= '<td>'.$r[$fields[$i]].'</td>';
    }

    // Adicionando botão de exclusão
    $table .= '<td><form action="banco/deleteC.php" method="post">'; //formulário com método post que vai para deleteF.php
    $table .= '<input type="hidden" name="ID" value="'.$r['ID'].'">';
    $table .= '<button  class="btn btn-danger">Excluir</button>'; //aqui está o seu botão
    $table .= '</form></td>';
}

//Finalizando a tabela
$table .= '</tbody></table>';

//Imprimindo a tabela
echo $table;

?>

The line being pointed at the error is:

$table .= '<th>'.$fields[$i].'</th>';

I don’t use PDO and I don’t understand what might be causing the error, someone could help me?

1 answer

0


The problem is that fetch_field_direct returns an object and you cannot concatenate that object with a string, for example.

To display the value you must use an attribute: Ex’s:

$fields[$i]->name
$fields[$i]->table
$fields[$i]->max_length
$fields[$i]->flags
$fields[$i]->type

Browser other questions tagged

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