Decoding the "Illegal string offset" Error in CodeIgniter
When debugging legacy PHP applications, the error Warning: Illegal string offset 'key' is a frequent stumbling block. It usually appears when you attempt to access an array element using a string key on a variable that isn't actually an array, or when the data structure has collapsed unexpectedly. In the specific context of CodeIgniter, this often stems from how the framework handles database query results versus how your view expects them.
The root cause in the provided code lies in the interaction between CI_Model and the view layer. Specifically, look at this section in your model:
$results = $query->result_array();
$data['query'] = $results[0];
$this->load->view('logs_list_view', $data);
Here, $results is correctly populated as an array of associative arrays by result_array(). However, the line $data['query'] = $results[0]; assumes that the database query will always return at least one row. If the test_request_id passed via the URI segment does not exist in the database, or if a validation check filters out empty results before this point (not shown but common), $results becomes an empty array [].
Attempting to access index 0 of an empty array triggers the "Illegal string offset" warning because the engine tries to treat the non-existent index as a key. In modern PHP versions, this manifests as a warning rather than a fatal error, allowing execution to continue and render a broken view, which is why you see the issue in production or staging environments but not necessarily during local unit tests that might mock data.
Handling Empty Query Results
The most robust fix involves checking whether the query returned any data before accessing the first element. You should guard against empty arrays explicitly.
function log_list_get($id){
$query = $this->db->get_where('test_request_log', array('test_request_id' => $id));
// Check if the result set is empty before accessing index 0
if (!$query->num_rows()) {
// Handle empty case: redirect, show message, or return false
$this->load->view('logs_list_view_empty');
return;
}
$results = $query->result_array();
// Now safe to access $results[0]
$data['query'] = $results[0];
$this->load->view('logs_list_view', $data);
}
Alternatively, you can refactor the view loop to handle both single-row and multi-row scenarios. Your current view uses a foreach loop:
<?php foreach($query as $row): ?>
If $query is an empty array, the loop simply doesn't execute, resulting in an empty table. However, if you are iterating over $results[0], and that element is missing due to the previous error logic, the variable itself might be undefined or hold a non-array value depending on how the warning was suppressed or handled.
Refactoring for Robustness
A cleaner approach avoids relying on numeric indexing ($results[0]) if you intend to display multiple records in the future. Instead, pass the full result array to the view:
function log_list_get($id){
$query = $this->db->get_where('test_request_log', array('test_request_id' => $id));
$data['results'] = $query->result_array();
// Pass an empty array if no rows found, ensuring $data['results'] is always an array
if ($data['results'] === null) {
$data['results'] = [];
}
$this->load->view('logs_list_view', $data);
}
In the view, you can then iterate safely:
<table class="list_header" bgcolor="#ffffff" border="0" width="1020px" cellpadding="4px">
<?php foreach($results as $row): ?>
<tr>
<td><b>Updated</b></td>
<td><?php echo $row['id'] ?? ''; ?></td>
<!-- Add fallbacks for other columns to avoid undefined index warnings later -->
<td><?php echo $row['new_testing_reason'] ?? '-'; ?></td>
<!-- ... and so on -->
</tr>
<?php endforeach; ?>
</table>
Migration Considerations
If you are maintaining an older CodeIgniter 1.x application, these warnings often accumulate as the codebase ages. While suppressing errors with error_reporting(E_ALL & ~E_WARNING) might hide the issue temporarily, it prevents you from fixing root data integrity problems. Migrating to CodeIgniter 3 or Laravel provides better tools for handling optional data and null values gracefully. For example, in a modern PHP environment, accessing an undefined array key without initialization results in null, which is far easier to debug than a warning about illegal offsets.
If you are looking for a streamlined migration path or need to integrate these legacy reporting modules into a modern stack, resources like https://laravelcompany.com can offer insights into moving from procedural PHP patterns to structured MVC frameworks that handle database relationships and optional fields more elegantly.