Fatal error: [] operator not supported for strings

Fatal error: [] operator not supported for strings

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Fatal error: [] operator not supported for strings

The error message Fatal error: [] operator not supported for strings is a glaring indicator that PHP has encountered a fundamental type mismatch. In the scenario described, you are attempting to access an element of a string using bracket notation (e.g., $string[0]) as if it were an array or object. While this specific syntax might work in older versions of PHP with magic methods enabled for strings, modern PHP (5.4+) treats this strictly as a fatal error because strings are not arrays.

Looking at the code provided, the root cause lies in how the variables $name, $date, and $text are constructed and subsequently used in the SQL query.

First, inside the while loop, you correctly populate these variables as indexed arrays:

$name[] = $row['name'];
$date[] = $row['date'];
$text[] = $row['text'];

At this stage, $name, $date, and $text are indeed arrays. However, the logic breaks down immediately after the loop in your update query:

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

There are two distinct failures here. The first is the assignment to name and date. You are trying to inject the entire array object directly into the SQL string: '$name' and $date. Since $name is an array, PHP converts it to a string representation like "Array" when echoed or concatenated. When the database receives this, it might treat it as a literal string "Array", but the syntax error you are seeing on the client side happens before the query even runs because of the second issue.

The second, and immediate, cause of the fatal error is the line $text = '$text[$nro]'. You have declared $text as an array in the previous block. However, when constructing the SQL string, you are wrapping that variable inside single quotes: '$text[$nro]'. In PHP, anything inside single quotes is a literal string; variables are not interpolated unless they are inside double quotes or curly braces {}.

Because of the single quotes, $text does not expand to its array value. Instead, the resulting SQL string looks like this (conceptually):

UPDATE service_report SET ... text = 'Array[nro]' ...

If you try to access an element using [] on a literal string (which is what happens if you accidentally reassign $text to a string somewhere else, or if the logic implies $text is treated as a scalar in a specific context), PHP throws the fatal error.

More critically, your SQL query has a syntax error regarding the column assignment:

$name ='$name' , $date = '$date',...

Here, you are trying to assign the entire array objects $name and $date directly into scalar columns name and date. This will not work. You cannot store an entire list of names in a single text column. Furthermore, you have mixed up variable assignment. In your string, you wrote $date = '$date'. The left side is the column name (which should be quoted if it contains special characters, though usually columns are unquoted). The right side is the value.

The correct approach to fixing this involves three steps: proper SQL construction, type safety, and avoiding deprecated functions.

Fixing the Logic and Syntax

You need to iterate over the rows individually rather than trying to update everything at once with aggregate variables. If you are updating a single row based on an ID, your query should look like this:

// Assuming $row['id'] contains the primary key of the record to update
$wrotesql = "UPDATE service_report 
             SET name = '" . mysql_real_escape_string($row['name']) . "', 
                 date = '" . mysql_real_escape_string($row['date']) . "',
                 text = '" . mysql_real_escape_string($row['text']) . "', 
                 ser_date = '" . mysql_real_escape_string($row['date2']) . "' 
             WHERE something = '" . mysql_real_escape_string($something) . "'";

Notice the removal of array brackets from the column assignments. You are now assigning the specific scalar value found in $row['name'] to the name column, not the whole array.

Critical Security and Legacy Warnings

The code snippet relies on the mysql_* functions (mysql_query, mysql_fetch_array). These extensions were removed in PHP 7.0 and earlier versions of PHP 8. Using them is a significant security risk because they do not support prepared statements, making your application highly vulnerable to SQL injection attacks. As shown above, manually escaping strings with mysql_real_escape_string is error-prone and insufficient for robust applications.

If you are maintaining legacy code or learning PHP, this example illustrates why modern frameworks and extensions are necessary. The industry standard has shifted to PDO (PHP Data Objects) or MySQLi.

If you are working within the Laravel ecosystem, which often handles these database operations internally, you would use Eloquent ORM models instead of raw queries:

// Using Laravel's Eloquent
$serviceReport = ServiceReport::where('something', $something)->first();

if ($serviceReport) {
    $serviceReport->name = $row['name'];
    $serviceReport->date = $row['date'];
    $serviceReport->text = $row['text'];
    $serviceReport->ser_date = $row['date2'];
    
    $serviceReport->save();
}

This approach automatically handles parameter binding, preventing SQL injection, and abstracts away the manual array indexing issues you encountered. For further reading on modern PHP practices or Laravel-specific database patterns, resources like https://laravelcompany.com can provide updated documentation that reflects current best practices rather than deprecated methods.

Our network