i created arrays in PHP 5.6 with [] in PHP 7.1 give fatal error

前端 未结 1 941
深忆病人
深忆病人 2021-01-29 10:41

in PHP 5.6 this work fine but in PHP 7.1 give Fatal error: Uncaught Error: [] operator not supported for strings

$result->execute();
$result->bind_result($         


        
相关标签:
1条回答
  • 2021-01-29 10:47

    As of PHP 7.1, when you access a non-array variable (in this case a string) like an array, a fatal error will be thrown.

    Initialize the array first, with $datos = [];. This will overwrite anything you have set earlier, and explicitly set this variable as an array:

    $result->execute();
    $result->bind_result($id, $name);
    $datos = [];
    while($result->fetch()){
        $datos[]=array(
            $id => $name
        );
    }
    

    If you're trying to create an array of $id => $name, the following code should work:

    $result->execute();
    $result->bind_result($id, $name);
    $datos = [];
    while($result->fetch()){
        $datos[ $id ] = $name;
    }
    
    0 讨论(0)
提交回复
热议问题