Error PHP Fatal error: [] operator not supported for strings in [closed]

0

error started appearing

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);
}
    
asked by anonymous 28.01.2018 / 18:41

1 answer

2

The error is being entered correctly.

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 , not strings.

Immediate solution:

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

... or exchange the beginning for this ...:

$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, simply rename one of the two variables.

    
28.01.2018 / 18:52