Warning: Illegal string offset

Asked

Viewed 25,706 times

5

I’m trying to show in the report the result of a INNER JOIN 3 tables, save the 3 values in a array and make a foreach to go through all the records, but the following error appears:

Warning: Illegal string offset 'Fleet'

Warning: Illegal string offset 'Full name'

Warning: Illegal string offset 'Description'

Man foreach is like this:

<?php foreach ($acessos as $acesso) : ?>
<tr>
    <td><?php echo $acesso['Frota']; ?></td>
    <td><?php echo $acesso['NomeCompleto']; ?></td>
    <td><?php echo $acesso['Descricao']; ?></td>
    <td class="actions text-right">
        <a href="Editar.php?Codigo=<?php echo $acesso['Codigo']; ?>" class="btn btn-sm btn-warning"><i class="fa fa-pencil"></i> Inserir Entrada</a>
    </td>
</tr>

This is code that makes the query in the database:

function INNERJOIN (){

    $database = open_database();
    $found = null;

    $sql = "SELECT tblacesso.Codigo, tblfrota.Frota, tblpessoa.NomeCompleto, tbldestino.Descricao FROM tblacesso INNER JOIN tblfrota ON(tblacesso.FrotaID = tblfrota.Codigo) INNER JOIN tblpessoa ON(tblacesso.MotoristaID = tblpessoa.Codigo) INNER JOIN tbldestino ON(tblacesso.DestinoID = tbldestino.Codigo)";
    $result = $database->query($sql);

    if ($result->num_rows > 0) {
        $found = $result->fetch_assoc();
    }    
    close_database($database);
    return $found;
}

I used a var_dump($acessos); to check the data type of my variable and the following result appeared:

array (size=3)
'Frota' => string '9999' (length=4)
'NomeCompleto' => string 'Jean C. Galhardi' (length=16)
'Descricao' => string 'Palmares' (length=8)//////

1 answer

4


This publication should be a "duplicate" of a few, I don’t even know if I should answer this, because the answer is in the error itself.

What is happening is because the foreach() is causing only one key to be accessed at a time.

You have it:

$array = ['Frota' => 9999, 'NomeCompleto' => 'Jean', 'Descricao' => 'Palmares'];

If you do this:

foreach($array as $valor){
     echo $valor;
}

The $item will be "9999" after "Jean" and then "Palmares" and not an array.

In a nutshell, in the way that I can explain, this function will cause it to obtain each key of the array, ie the loop is by key, in this case holds three keys, each "time it rotates" will pick a value of the respective key.

To use as you want you need to make an array inside the other, this way:

$array = [
    0 => ['Frota' => 9999, 'NomeCompleto' => 'Jean', 'Descricao' => 'Palmares']
];

Thus the 0 =>, which may be omitted or otherwise, is the key, so use the same foreach will cause the $valor be an array (containing Fleet, Full Name, Description).

Logo, using this new array with this (which you are using):

foreach($array as $item){
    echo $item['Frota'];
    echo PHP_EOL;
    echo $item['NomeCompleto'];
}

It will work normally because it will get the value of the key 0 which is the array, this array will be accessed by $item['Frota'] for example.

If you do not keep array as is and just remove the foreach, will work normally.


Change this:

if ($result->num_rows > 0) {
  $found = $result->fetch_assoc();
}    

For this:

if ($result->num_rows > 0) {
  while($linha = $result->fetch_assoc()){
     $found[] = $linha;
  }
}    

The rest remains like this, this will create an array within the other, fixing the problem of foreach the only caution is that if not found will return null, due to $found = null;.

Another option, if using Mysqlnd is to use:

if ($result->num_rows > 0) {
  $found = $result->fetch_all(MYSQLI_ASSOC);
}  
  • Thanks for the answer... I’m beginner in PHP, help me to schematize, you put the spread code to explain and I ended up getting lost, how will the code?

  • Need because it can have more than one record to appear on the screen.

  • I edited my question with the code that makes JOIN. I use foreach to list the results.

  • The fetch_assoc will always return a single line, only using loop or the fetch_all will return all, I will edit the answer.

  • It worked... thanks for the availability.

Browser other questions tagged

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