Skip to content

Add is_nullable property to field_data() results in the MySQLi driver #6335

Description

@hanifigoktas

Feature Request:
Add is_nullable to field_data() output (MySQLi driver)

Summary:
$this->db->field_data($table) currently returns objects with name, type, max_length, primary_key, and default — but no information about whether a column is nullable (NOT NULL vs NULL). This makes it impossible to build generic, metadata-driven forms that correctly mark required vs. optional fields without a second, manual SHOW COLUMNS / information_schema query.

Motivation:
We built a generic CRUD form renderer that iterates over field_data($table) to render each column's label and input, and needs to know per-column nullability to:

mark required fields in the UI (asterisk / required attribute)
decide default value / validation behavior
Since field_data() already runs SHOW COLUMNS FROM

(MySQLi driver) — which includes a Null column (YES/NO) — and MySQLi's native mysqli_result::fetch_fields() already exposes an is_nullable flag per field, this data is available "for free" with no extra query. It's just not being surfaced in the return object.

Current behavior:
system/database/drivers/mysqli/mysqli_driver.php::field_data() builds each $retval[$i] from the SHOW COLUMNS result but stops at primary_key:

$retval[$i]->default = $query[$i]->Default;
$retval[$i]->primary_key = (int) ($query[$i]->Key === 'PRI');
system/database/drivers/mysqli/mysqli_result.php::field_data() builds each $retval[$i] from mysqli_result::fetch_fields() and also stops before using the native is_nullable flag that MySQLi already provides:

$retval[$i]->default = $field_data[$i]->def;
Proposed change

Add one line to each of the two functions above:

mysqli_driver.php (field_data($table), based on SHOW COLUMNS):

$retval[$i]->default = $query[$i]->Default;
$retval[$i]->primary_key = (int) ($query[$i]->Key === 'PRI');
$retval[$i]->is_nullable = (int) ($query[$i]->Null === 'YES');
mysqli_result.php (field_data(), based on mysqli_result::fetch_fields()):

$retval[$i]->default = $field_data[$i]->def;
$retval[$i]->is_nullable = $field_data[$i]->is_nullable;
Backward compatibility

Purely additive — adds a new property to the returned stdClass objects, does not change or remove any existing property, and does not alter the method signature. Existing code that ignores the new property is unaffected.

Scope note

This report focuses on the MySQLi driver since it's what we use in production, but the same one-line addition (surfacing the underlying "is this column nullable" data that's already fetched, just not exposed) is applicable to every other supported database driver in CodeIgniter 3 as well.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions