[php] Fatal error: [] operator not supported for strings

I'm getting information from database, saving it in array and echoing it in a form with loop structure and I'm having problems when I try to save the modified information to database.

I'm getting this error:

Fatal error: [] operator not supported for strings in....

Code:

$namesql1 = "SELECT name,date,text,date2 FROM table WHERE something= '$something'";
$nameresult1 = mysql_query($namesql1);
$countrows = mysql_num_rows($nameresult1);
while ($row = mysql_fetch_array($nameresult1, MYSQL_ASSOC)) {
    $name[] = $row['name'];
    $date[] = $row['date'];
    $text[] = $row['text'];
    $date2[] = $row['date2 '];
}

/** SOME CODE HERE **/

    
$wrotesql = "UPDATE service_report SET  name ='$name' , $date = '$date',$text = '$text[$nro]', ser_date = '$date2[$nro]' WHERE something = '$something')";

$wroteresult = mysql_query($wrotesql);

Could somebody please give me a hint what I'm doing wrong?

This question is related to php mysql

The answer is


Solved!

$a['index'] = [];
$a['index'][] = 'another value';
$a['index'][] = 'another value';
$a['index'][] = 'another value';
$a['index'][] = 'another value';

I agree with Jeremy Young's comment on Phils answer:

I have found that this can be a problem associated with migrating from php 5 to php 7. php 5 was more tolerant of amibiguity in whether a variable was an array or not than php 7 is. In most cases the solution is to declare the array explicitly, as explained in this answer.

I was just trouble shooting a Wordpress plugin after the migration of php5 to php7. Since the plugin code was relying on user input, and it was intrinsically used in the code either as string, or as array, I added the following code in to prevent a fatal error:

if(is_array($variable_either_string_or_array)){
    // if it's an array, declaration is allowed:
    $variable_either_string_or_array[]=$additionalInfoData[$i];
}else{
    // if it's not an array, declaration it as follows:
    $variable_either_string_or_array=$additionalInfoData[$i];
}

This was the only modification I needed to add to make the plugin php7-proof. Obviously not "best practices", I'd rather read and understand the full code.. but a quick fix was needed.


Such behavior is described in Migrating from PHP 7.0.x to PHP 7.1.x/

The empty index operator is not supported for strings anymore Applying the empty index operator to a string (e.g. $str[] = $x) throws a fatal error instead of converting silently to array.

In my case it was a mere initialization. I fixed it by replacing $foo='' with $foo=[].

$foo='';
$foo[]='test';
print_r($foo);

You have probably defined $name, $date, $text or $date2 to be a string, like:

$name = 'String';

Then if you treat it like an array it will give that fatal error:

$name[] = 'new value'; // fatal error

To solve your problem just add the following code at the beginning of the loop:

$name = array();
$date = array();
$text = array();
$date2 = array();

This will reset their value to array and then you'll able to use them as arrays.


I had similar situation:

$foo = array();
$foo[] = 'test'; // error    
$foo[] = "test"; // working fine


this was available in php 5.6 in php 7+ you should declare the array first

$users = array(); // not $users = ";
$users[] = "762";