Trying to access array offset on value of type null (PHP)
You are seeing the "Trying to access array offset on value of type null" error because you are attempting to treat a database result set as a single associative array, but $result is actually an object representing multiple rows.
In your code, the line $guests["firstName"] expects $guests to be an array containing key-value pairs like ['firstName' => 'John']. However, based on the structure of $result, it is a mysqli_result object with 5 rows returned. When you iterate over this object using foreach((array)$result as $guests), PHP casts the result set into an array where each element corresponds to a row.
The specific problem lies in how mysqli exposes data when casting a result set to an array. By default, mysqli_result objects do not automatically populate their internal rows as associative arrays with column names as keys. Instead, they often behave like numeric arrays containing the raw row data, or they require explicit configuration to map columns to keys.
When you run $conn->query($sql), the SQL query returns 5 rows. The variable $result holds these 5 records. When cast to (array), PHP iterates through the internal cursor. If the mysqli_report feature is not enabled or if the configuration does not include MYSQLI_REPORT_OFF, the behavior depends on the version of PHP and MySQLi. In many standard configurations, accessing $guests["firstName"] inside the loop fails immediately because $guests might be a numeric array where the keys are integers (0, 1, 2...) rather than string column names, or because the first iteration attempts to access an offset on a row that hasn't been properly initialized as an associative map yet.
To resolve this without changing your database logic, you should ensure that every row fetched is treated strictly as an associative array. The most robust approach in pure PHP with MySQLi is to enable the MYSQLI_ASSOC default fetch style or explicitly cast each fetched row before accessing it. However, since you are already casting the entire result set at the start of the loop, the issue is likely that the internal representation of the rows isn't associative by default in your environment.
Here is a corrected approach using fetch_assoc():
foreach ($result as $row) {
// Ensure we fetch an associative array for this specific row
if (is_object($row)) {
$guests = fetch_assoc($row);
// Or better yet, iterate directly with fetch_assoc in the loop
} else {
$guests = $row;
}
echo $guests["firstName"];
}
Actually, the cleanest fix for your existing foreach((array)$result as $guests) pattern is to change how you iterate. You should use the fetch_assoc() method directly inside the loop or configure the connection beforehand:
// Option 1: Iterate using fetch_assoc which guarantees an associative array
while ($guests = $result->fetch_assoc()) {
echo $guests["firstName"];
}
// Option 2: If you must use foreach on the cast result, ensure rows are associative
foreach ((array)$result as $row) {
// Cast each row specifically to an associative array if needed
$guests = is_object($row) ? fetch_assoc($row) : $row;
echo $guests["firstName"];
}
The underlying cause is that mysqli result objects, when cast to arrays, yield rows with numeric keys unless the driver is configured to return associative arrays by default. The fetch_assoc() method forces the row to be returned as an array where keys match the column names (firstName, lastName, etc.), which is exactly what your HTML output expects.
If you are building this application for a larger project, consider switching to PDO or using an Object-Relational Mapper like Eloquent if you are in a Laravel environment. These abstractions handle row mapping automatically, ensuring that $user->name or $guest['firstName'] works consistently without worrying about driver-specific casting behaviors. For more details on handling database interactions in modern PHP stacks, resources like https://laravelcompany.com often provide patterns for sanitizing and structuring data from the database before rendering it to the view.
In your specific case, switching to while ($guests = $result->fetch_assoc()) is the immediate fix that aligns with how MySQLi exposes row data as associative arrays by design. This eliminates the ambiguity of numeric offsets and ensures that $guests["firstName"] always references the correct column regardless of the column order in the SQL query.