PHP Fatal error: [] Operator not supported for strings in

Asked

Viewed 2,576 times

0

error started to appear

PHP Fatal error: [] Operator not supported for strings in

$data = date('Y-m-d');

$q = mysqli_query($con, "SELECT * FROM agendasalao WHERE dataReserva >= $data ORDER BY dataReserva ASC");


$qtde_registros = mysqli_num_rows($q);
if ($qtde_registros > 0) {
    while ($row = mysqli_fetch_object($q)){
        $data[] = $row;  <<< erro aqui
    }
    echo json_encode($data);
}
  • puts... this variable ta wrong even. silly error. I will change to $dateAtual .

  • Just one detail, building queries using the joker *, is a bad idea for code performance. Try to rescue only the attributes needed to run the routine by setting in your clause.

1 answer

2


The error is being correctly indicated.

You create a string at the beginning of the code:

$data = ...

That is, a string. In the loop you want to add items:

$data[] = ...

Items are added in arrays, and not in strings.

Immediate solution:

If it really is to accumulate everything in the same variable...

... or exchange the beginning for it...:

$data[] = date('Y-m-d');

... Or adapt in the loop:

$data .= $row-> (aqui você poe o item desejado do objeto retornado); 

If this is not the intention, just change the name of one of the two variables.

Browser other questions tagged

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